c - 由于正则表达式中的 malloc(0) 导致程序中止

标签 c regex linux

谁能告诉我为什么我的程序中止?我正在使用 efence 编译它,它会在 malloc(0) 上中止,正如 GDB 回溯所示,regcomp 正在执行 malloc(0)

1218 void extractTime(int extractStartTime)
1219 {
1220         char  * charPtr, * numberFormatErr;
1221         regex_t re;
1222 
1223         ( extractStartTime == 1 ) ? ( charPtr = getenv("EF_ERRTRACK_START") ) :
1224                 ( charPtr = getenv("EF_ERRTRACK_END") );
1225 
1226         if ( charPtr == NULL )
1227                 return;
1228 
1229         double envVal = strtod(charPtr, &numberFormatErr);
1230 
1231         if ( (numberFormatErr == charPtr) || (*numberFormatErr != '\0') ) {
1232                 ( extractStartTime == 1 ) ? EF_Print("eFence exited: EF_ERRTRACK_START is not a number\n") :
1233                         EF_Print("eFence exited: EF_ERRTRACK_END is not a number\n");
1234                 exit(1);
1235         }
1236         else if ( envVal < 0 ) {
1237                 ( extractStartTime == 1 ) ? EF_Print("eFence exited: EF_ERRTRACK_START a negative number\n") :
1238                         EF_Print("eFence exited: EF_ERRTRACK_END is a negative number\n");
1239                 exit(1);
1240         }
1241 
1242         /* If we are here then it is a valid number, now lets check if it is exponential or not */
1243 
1244         regcomp(&re, "^([0-9]+[.]?[0-9]*|[0-9]*[.][0-9]+)[eE][+-]?[0-9]+$", REG_EXTENDED);
1245 
1246         if ( regexec(&re, charPtr, 0, 0, 0) == 0 )
1247         {
1248                 /* It is an exponential number, then already parsed by strtod earlier*/
1249                 sprintf(charPtr, "%lf", envVal);
1250         }
1251 

这是 GDB 回溯:

(gdb) r
Starting program: /tmp/efence/ikatrack1_dev 
[Thread debugging using libthread_db enabled]

Electric Fence 2.1 Copyright (C) 1987-1998 Bruce Perens.

ElectricFence Aborting: Allocating 0 bytes, probably a bug.

[New Thread 0x4001e350 (LWP 1528)]

Program received signal SIGILL, Illegal instruction.
[Switching to Thread 0x4001e350 (LWP 1528)]

0x4008734c in kill () from /devel/lib/libc.so.6

(gdb) bt
#0  0x4008734c in kill () from /devel/lib/libc.so.6
#1  0x0000b86c in EF_Abort (pattern=0x1000 <Address 0x1000 out of bounds>)
at print.c:137
#2  0x00009564 in memalign (alignment=4, userSize=0) at efence.c:533
#3  0x0000a5bc in malloc (size=0) at efence.c:1027
#4  0x400fe5bc in re_node_set_alloc (set=0x4025cfd8, size=0)
at regex_internal.c:959
#5  0x400ff2ac in register_state (dfa=0x25, newstate=0x4025cfc8, hash=86528)
at regex_internal.c:1550
#6  0x40102d64 in re_acquire_state_context (err=0xbebd7b88, dfa=0x40196f74, 
nodes=0xbebd7b74, context=0) at regex_internal.c:1706
#7  0x4010c060 in re_compile_internal (preg=0xbebd7bf0, 
pattern=0xcb74 "^([0-9]+[.]?[0-9]*|[0-9]*[.][0-9]+)[eE][+-]?[0-9]+$", 
length=<value optimized out>, syntax=242428) at regcomp.c:989
#8  0x4010d5f8 in __regcomp (preg=0xbebd7bf0, 
pattern=0xcb74 "^([0-9]+[.]?[0-9]*|[0-9]*[.][0-9]+)[eE][+-]?[0-9]+$", 
cflags=1) at regcomp.c:480
#9  0x0000ae1c in extractTime (extractStartTime=1) at efence.c:1244
#10 0x0000aa1c in efence_ctor () at efence.c:1144
#11 0x0000c528 in __libc_csu_init (argc=1, argv=0xbebd7de4, envp=0xbebd7dec)
at elf-init.c:83
#12 0x40070fe8 in __libc_start_main (main=0x8c54 <main>, argc=1, 
ubp_av=0xbebd7de4, init=0xc4d0 <__libc_csu_init>, 
---Type <return> to continue, or q <return> to quit---
fini=0xc4c0 <__libc_csu_fini>, rtld_fini=0x4000ea50 <_dl_fini>, 
stack_end=0xbebd7de4) at libc-start.c:179
#13 0x00008bcc in _start ()

从 GDB 回溯中,我可以看到问题出在第 4 帧:

#4 0x400fe5bc in re_node_set_alloc (set=0x4025cfd8, size=0) 它正在执行 malloc(0) 但它是怎么来的以及为什么这样做?

最佳答案

更改您的电围栏设置 (EF_ALLOW_MALLOC_0) 以忽略此问题。

malloc(0) 不是非法的。再次阅读错误消息,它显示“可能是一个错误”。它会中止,以便您进行调查。一旦确定它实际上不是错误,请跳过它并继续运行。

关于c - 由于正则表达式中的 malloc(0) 导致程序中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7502178/

相关文章:

javascript - 组匹配之间的正则表达式匹配文本

python - 在正则表达式中查找匹配的文本

c++ - Linux 上的预编译头警告(太短,无法成为 PCH 文件)

linux - 从文件夹中的选择文件类型创建符号链接(symbolic link)

regex - 使用 Bash 从文件名中的特定位置移动数字

c - 数组指针与普通指针的区别

c - UTF-8 到 Unicode 转换

c - XCode C 项目不显示文件夹 "Tree"

linux - 用于递归查找和替换的 Sed 命令失败

c - 如何从全局内存或纹理内存中获取 16 字节