简体   繁体   English

C sscanf i386 与 x86_64 解析引用字符串的不同行为

[英]C different behavior of sscanf i386 vs x86_64 parsing quoted string

I have found strange behavior of sscanf parsing double quoted string on i386 machine.我发现sscanf在 i386 机器上解析双引号字符串的奇怪行为。

My code:我的代码:

char string[] = "\"1597821828\" \"MODIFY\" \"/fullpath/test2\"";
printf("line : %s", string);
int total;
char store_date[12] = "", store_etat_watch[24] = "", store_path[1024] = "";
printf("stores init:%s,%d , %s,%d , %s,%d \n",
       store_date, strlen(store_date),
       store_etat_watch, strlen(store_etat_watch),
       store_path, strlen(store_path));
total = sscanf(string,"%s %s %s", store_date, store_etat_watch, store_path);
printf("stores brut:%s,%d , %s,%d , %s,%d \n",
       store_date, strlen(store_date),
       store_etat_watch, strlen(store_etat_watch),
       store_path, strlen(store_path));

I compile it on a debian arm64 an execute it (here is my Makefile):我在 debian arm64 上编译它并执行它(这是我的 Makefile):

CC = gcc CC = gcc

ERROR_CFLAGS = -Wall -W -pedantic -Wextra -Werror CFLAGS = -static -g -O0 $(ERROR_FLAGS) -D_REENTRANT -DLinux LDFLAGS = $(ERROR_FLAGS) ERROR_CFLAGS = -Wall -W -pedantic -Wextra -Werror CFLAGS = -static -g -O0 $(ERROR_FLAGS) -D_REENTRANT -DLinux LDFLAGS = $(ERROR_FLAGS)

all: sscanf_test全部:sscanf_test

sscanf_test: sscanf_test.c $(CC) $(CFLAGS) sscanf_test.c -o sscanf_test.PHONY:clean sscanf_test: sscanf_test.c $(CC) $(CFLAGS) sscanf_test.c -o sscanf_test.PHONY:clean

clean: rm -f sscanf_test清洁:rm -f sscanf_test

result:结果:

line: "1597821828" "MODIFY" "/fullpath/test2"行:“1597821828”“修改”“/fullpath/test2”

stores init:,0, ,0, ,0存储 init:,0, ,0, ,0

stores brut:"1597821828",12, "MODIFY",8, "/fullpath/test2",17存储 brut:"1597821828",12, "MODIFY",8, "/fullpath/test2",17

That's the result I expect.这就是我所期望的结果。 Then I compile it on ubuntu i386 and execute it:然后我在 ubuntu i386 上编译并执行:

result:结果:

line: "1597821828" "MODIFY" "fullpath/test2"行:“1597821828”“修改”“完整路径/test2”

stores init:,0, ,0, ,0存储 init:,0, ,0, ,0

stores brut: "1597821828""MODIFY",20 , "MODIFY",8,"/fullpath/test2",17商店 brut: "1597821828""MODIFY",20 , "MODIFY",8,"/fullpath/test2",17

The first string is not correctly parsed.第一个字符串未正确解析。 If the string doesn't begin with double quote: 'no problem'.如果字符串不以双引号开头:'没问题'。

char string[]="1597821828 \"MODIFY\" \"/fullpath/test2\"";

result:结果:

line: "1597821828" "MODIFY" "/fullpath/test2"行:“1597821828”“修改”“/fullpath/test2”

stores init:,0, ,0, ,0存储 init:,0, ,0, ,0

stores brut:1597821828,10, "MODIFY",8, "/fullpath/test2",17商店 brut:1597821828,10, "MODIFY",8, "/fullpath/test2",17

I do have to parse string beginning with double quote.我必须解析以双引号开头的字符串。

Somebody can help me?有人可以帮助我吗?

char store_date[12] is too small to store the the string *1597821828* (Quote character replaced by * for clarity.). char store_date[12]太小,无法存储字符串*1597821828* (为清楚起见,引号字符替换为* 。)。 Result: undeifned behavior (UB).结果:未定义的行为(UB)。 A string of length 12 needs at least 12+1 char .长度为 12 的字符串至少需要 12+1 char The +1 is for the null character . +1 用于null 字符

That it seemed to work on some systems is normal for UB;它似乎在某些系统上工作对于 UB 来说是正常的; UB isn't "required to fail", it's " anything can happen". UB 不是“必须失败”,而是“任何事情都可能发生”。 If the memory you stepped on outside store_data[12] happened to not be something the compiler was using for anything needed later in the function, it can happen to work.如果您在store_data[12]之外踩到的 memory 碰巧不是编译器用于稍后在 function 中需要的任何东西,它可能会发生工作。 That's what makes undefined behaviour so nasty: testing can't always reveal it.这就是使未定义行为如此讨厌的原因:测试不能总是揭示它。


Make the destination array large enough and use widths with "%s" .使目标数组足够大,并使用带有"%s"宽度。

// char store_date[12]="";
// total=sscanf(string,"%s",store_date);

char store_date[12+1]="";
total=sscanf(string,"%12s",store_date);

Mis-matched specifier with strlen()strlen()不匹配的说明符

// printf("%d \n",strlen(store_path));
printf("%zu\n",strlen(store_path));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从i386移动到x86_64时的浮点精度 - Floating-point precision when moving from i386 to x86_64 timer_create在i386系统上导致分段错误,但在x86_64系统上未引起分段错误(linux) - timer_create causing segmentation fault on i386 system, but not x86_64 system(linux) c ) 出错和链接问题:输入文件的 i386:x86-64 架构,与 i386 不兼容 output - c )make error& link problem: i386:x86-64 architecture of input file, incompatible with i386 output CMake; 386:x86-64 输入文件架构 (..) 与 i386 不兼容 output - CMake; 386:x86-64 architecture of input file (.. ) is incompatible with i386 output 链接问题:i386:x86-64输入文件架构* .o与i386输出不兼容 - linking problem: i386:x86-64 architecture of input file *.o is incompatible with i386 output GCC:输入文件“ ../window.ui.o”的i386体系结构与i386:x86-64输出不兼容 - GCC: i386 architecture of input file `../window.ui.o' is incompatible with i386:x86-64 output 文件是为i386构建的,而不是在Mac OSX 10.6上为iOS 4.2编译OpenCV2.2时所链接的架构(x86_64) - file was built for i386 which is not the architecture being linked (x86_64) while compiling OpenCV2.2 for iOS 4.2 on Mac OSX 10.6 堆栈eip溢出x86 vs x86_64 easy C代码 - Stack eip overflow x86 vs x86_64 easy C code x86与x86_64调用约定 - x86 vs x86_64 calling convention 我如何在编译时在 gcc 中识别 x86 与 x86_64? - How do I identify x86 vs. x86_64 at compile time in gcc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM