GCC는 오류 또는 경고를 반환하지 않습니다. 암호를 21 자 이상 입력하면 프로그램이 분리됩니다. 는 처음에 나는 있었다 : 신호 SIGSEGV 수신 : vfprintf.c : 해당 파일이나 디렉토리가 없습니까?
hash_p = SuperFastHash(query.pwd, strlen(query.pwd));
는 GCC에서이 경고를 얻을 : 해쉬 함수는 int를 반환하기 때문에
sprintf(hash_p, "%d", SuperFastHash(query.pwd, strlen(query.pwd)));
:
warning: assignment makes pointer from integer without a cast [-Wint- conversion]
hash_p = SuperFastHash(query.pwd, strlen(query.pwd));
^
그래서 난으로 변경되었습니다. strcmp는 두 유형의 const char *를 비교합니다. 내가 GDB를 사용하여 프로그램을 디버깅 할 때
는,이를 반환
Program received signal SIGSEGV, Segmentation fault.
0xb7e500d5 in _IO_vfprintf_internal (s=<optimized out>, format=<optimized out>,
ap=0xbffff46c "\261\[email protected]") at vfprintf.c:1636
1636 vfprintf.c: No such file or directory.
프로그램 :
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdint.h>
#define BUF_SIZE 1024
#undef get16bits
#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
|| defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
#define get16bits(d) (*((const uint16_t *) (d)))
#endif
#if !defined (get16bits)
#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
+(uint32_t)(((const uint8_t *)(d))[0]))
#endif
uint32_t SuperFastHash (const char * data, int len) {
uint32_t hash = len, tmp;
int rem;
if (len <= 0 || data == NULL) return 0;
rem = len & 3;
len >>= 2;
for (;len > 0; len--) {
hash += get16bits (data);
tmp = (get16bits (data+2) << 11)^hash;
hash = (hash << 16)^tmp;
data += 2*sizeof (uint16_t);
hash += hash >> 11;
}
switch (rem) {
case 3: hash += get16bits (data);
hash ^= hash << 16;
hash ^= ((signed char)data[sizeof (uint16_t)]) << 18;
hash += hash >> 11;
break;
case 2: hash += get16bits (data);
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1: hash += (signed char)*data;
hash ^= hash << 10;
hash += hash >> 1;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
struct log_n {
char uid[BUF_SIZE];
char pwd[BUF_SIZE];
};
struct log_n acc[1] = {
"username","-1257730142"
};
struct log_n query;
int main() {
char *hash_p;
do {
do{
printf("Username: ");
fgets(query.uid, sizeof query.uid, stdin);
query.uid[strcspn(query.uid, "\n")] = '\0';
if (strlen(query.uid) < 4) {
printf("Username must be between four and eight characters.\nTry again.\n");
}
if (strlen(query.uid) > 8) {
printf("Username must be less than eight characters.\nTry again.\n");
}
} while (strlen(query.uid) < 4 || strlen(query.uid) > 8);
do{
printf("Password: ");
fgets(query.pwd, sizeof query.pwd, stdin);
query.pwd[strcspn(query.pwd, "\n")] = '\0';
sprintf(hash_p, "%d", SuperFastHash(query.pwd, strlen(query.pwd)));
if (strlen(query.pwd) < 21) {
printf("Password must be atleast twenty-one characters long.\nTry again.\n");
}
} while (strlen(query.pwd) < 21);
} while (strcmp(query.uid, acc->uid) !=0 || strcmp(hash_p, acc->pwd) !=0);
}
참고 :이 숙제가 아닙니다. 나는 인터넷을 사용하는 사람들의 도움으로 C를 배우는 사람 일뿐입니다. 이 라인에서
숙제가 아니라는 점은 잘 알고 있지만 숙제 문제를 정말 환영한다는 것을 알고 있어야합니다. 이것이 숙제를위한 것이라면, 여전히 주제가 될 것입니다. 분명히 사전에 노력을 기울여 명확하게 명시된 문제가 있으며, 사람들은 그러한 경우에 기꺼이 도와줍니다. 우리가 낙담하는 숙제 문제는 노력이없는 곳이나 질문 작성자가 누군가가 속일 수 있도록 돕는 것이 좋습니다. – halfer