C\UNIX\strcmp 第一次使用是错误的,其他次都正确
嘿,我在 microsoft VS 上写了一些代码,它应该将输入的密码与数据库中存储的密码进行比较,并返回批准或拒绝... 它在 Windows 上工作得很好,但是在转换到 UNIX(使用 Eclipse)之后,发生了一件有趣的事情 - 总是,第一次调用这个函数不会返回批准的值,但它应该返回批准的值,但再次调用该函数时会返回完全相同的参数批准...如所希望的。 调试后,我非常确定问题出在“strcmp”中,它在第一次运行时返回 false,在完全相同的参数的所有其他运行中返回 true。
任何人都知道可能是什么问题?
命令示例: 添加jt 111 // 添加数据库
登录密码 jt 111
<块引用>拒绝
登录 jt 111
<块引用>已批准
void login_helper(char *user, char *password){
int found = 0;
int i;
for (i=0 ; i<space ; i++){
if (strcasecmp(data[i].name,user) == 0) {
found = 1;
if (strcmp(data[i].hash ,Md5FromString(password)) == 0)
{
printf("approved.\n");
break;
}
else {
printf("denied.\n");
break;
}
}
}
if (found == 0) printf("denied.\n");
}
hey all i wrote some code on microsoft VS which is suppose to compare passwords entered to ones stored in database and return approved or denied...
it worked perfectly good on windows, but after converting to UNIX (using eclipse) a funny thing happend - always, the first call to this function doesnt return the approved value when it should, but calling for the function again with exactly the same params returns approved... as desired.
after debugging i am pretty sure the problem is in the "strcmp", that returns false on the first run and true in all other runs on the exact same parameters.
anyone has an idea on what could be the problem??
an example for a commands:
add jt 111
// adding the password to the DB
login jt 111
denied
login jt 111
approved
void login_helper(char *user, char *password){
int found = 0;
int i;
for (i=0 ; i<space ; i++){
if (strcasecmp(data[i].name,user) == 0) {
found = 1;
if (strcmp(data[i].hash ,Md5FromString(password)) == 0)
{
printf("approved.\n");
break;
}
else {
printf("denied.\n");
break;
}
}
}
if (found == 0) printf("denied.\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我预测对
Md5FromString(password)
的调用会返回一个指向缓冲区的指针,当Md5FromString()
函数返回时,该指针不再有效。这意味着您会遇到未定义的行为,并且在某些情况下很幸运,而在其他情况下则很不幸。将代码发布到
Md5FromString()
。I predict that the call to
Md5FromString(password)
returns a pointer to a buffer that's no longer valid when theMd5FromString()
function returns. That would mean that you're running into undefined behavior, and getting lucky in some cases and unlucky in others.Post the code to
Md5FromString()
.我真的怀疑strcmp()有什么问题。 :-)
(有一本关于软件开发的优秀书籍,名为“The Pragmatic Programmer”,作者是 Andrew Hunt 和 David Thomas,其中有一个关于调试的技巧,称为“‘select’没有被破坏”,这最终意味着一个软件真的不太可能基本系统功能(例如 select() 或 strcmp())已损坏。)
您之前是否尝试过 printf'ing 'data[i].hash' 的内容以及 'Md5FromString(password)' 返回的值strcmp()?
比如:
另外,谁为函数 Md5FromString() 分配内存?您能发送 Md5FromString() 的代码吗?
干杯,
保罗
I'd really doubt there's any problem in strcmp(). :-)
(There's an excellent book on SW development called "The Pragmatic Programmer", by Andrew Hunt and David Thomas, which has a tip regarding debugging called "'select' is not broken", which ultimately means that it's really unlikely that a basic system function (e.g. select() or strcmp()) is broken.)
Did you try printf'ing the contents of 'data[i].hash' and the value returned by 'Md5FromString(password)' right before strcmp()?
Something like:
Also, who allocates memory for function Md5FromString()? Can you send the code for Md5FromString()?
Cheers,
Paulo