gpt4 book ai didi

c - "null pointer"尝试操作字符串时出错

转载 作者:行者123 更新时间:2023-11-30 21:22:18 25 4
gpt4 key购买 nike

我正在用 C 语言开发一个简单的程序来破解哈希输入,但是我无法检测输入是否有效。基本上我只是想在破解哈希之前检测用户输入的字符串是否有效。这是代码:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isHashValid(char *string);

int main(void){
char *userInput = NULL;

printf("enter hash key: ");
scanf("%s\n", userInput);
isHashValid(userInput);

return 0;
}

bool isHashValid(char *string){
int size = strlen(string);

for(int i = 0; i < size; i++){
if(string[i] == ' '){
printf("space detected\n");
return false;
}
}

printf("hash is valid!\n");
return true;
}

这是完整的错误消息:

 runtime error: null pointer passed as argument 1, which is declared to never be null
/usr/include/string.h:385:33: note: nonnull attribute specified here
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==819==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f0de173e5a1 bp 0x7ffdc9ea3440 sp 0x7ffdc9ea33c8 T819)
==819==The signal is caused by a READ memory access.
==819==Hint: address points to the zero page.
#0 0x7f0de173e5a0 (/lib/x86_64-linux-gnu/libc.so.6+0x18e5a0)
#1 0x427fd8 (/home/ubuntu/crack+0x427fd8)
#2 0x427f8b (/home/ubuntu/crack+0x427f8b)
#3 0x7f0de15d1b96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#4 0x402af9 (/home/ubuntu/crack+0x402af9)

UndefinedBehaviorSanitizer can not provide additional info.
==819==ABORTING

最佳答案

您实际上并未将内存分配给 userImput。由于它是一个字符串,您需要使用 malloc,或者执行字符用户输入[50]为了给你的字符串分配一些空间

关于c - "null pointer"尝试操作字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57245304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com