由于正则表达式中的malloc(0),程序正在中止

由于正则表达式中的malloc(0),程序正在中止,c,regex,linux,C,Regex,Linux,有人能告诉我为什么我的程序要终止吗?我用efence编译它,它在malloc(0)上中止,正如GDB回溯所表明的,regcomp正在执行malloc(0) 1218无效提取时间(int-extractStartTime) 1219 { 1220字符*charPtr,*numberformer; 1221正则表达式; 1222 1223(extractStartTime==1)?(charPtr=getenv(“EF\u ERRTRACK\u START”): 1224(charPtr=geten

有人能告诉我为什么我的程序要终止吗?我用efence编译它,它在malloc(0)上中止,正如GDB回溯所表明的,regcomp正在执行malloc(0)

1218无效提取时间(int-extractStartTime)
1219 {
1220字符*charPtr,*numberformer;
1221正则表达式;
1222
1223(extractStartTime==1)?(charPtr=getenv(“EF\u ERRTRACK\u START”):
1224(charPtr=getenv(“EF_ERRTRACK_END”);
1225
1226如果(charPtr==NULL)
1227返回;
1228
1229双环境=strtod(字符和数字格式);
1230
1231如果((numberformater==charPtr)| |(*numberformater!='\0')){
1232(extractStartTime==1)?EF_打印(“eFence退出:EF_ERRTRACK_START不是一个数字\n”):
1233 EF_Print(“eFence退出:EF_ERRTRACK_END不是数字\n”);
1234出口(1);
1235         }
1236其他条件(环境值<0){
1237(extractStartTime==1)?EF_打印(“eFence退出:EF_ERRTRACK_开始一个负数\n”):
1238 EF_Print(“eFence EXIT:EF_ERRTRACK_END为负数\n”);
1239出口(1);
1240         }
1241
1242/*如果我们在这里,那么它是一个有效数字,现在让我们检查它是否为指数*/
1243
1244 regcomp(&re,“^([0-9]+[.]?[0-9]*.[0-9]*.[.][0-9]+)[eE][+-]?[0-9]+$”,注册扩展);
1245
1246 if(regexec(&re,charPtr,0,0)=0)
1247         {
1248/*这是一个指数数字,之前strod已经对其进行了解析*/
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)r
启动程序:/tmp/efence/ikatrack1\u dev
[已启用使用libthread_db的线程调试]
电气围栏2.1版权所有(C)1987-1998 Bruce Perens。
正在中止:分配0字节,可能是错误。
[新螺纹0x4001e350(LWP 1528)]
程序收到信号信号,指令非法。
[切换到线程0x4001e350(LWP 1528)]
/devel/lib/libc.so.6中kill()中的0x4008734c
(gdb)英国电信
#来自/devel/lib/libc.so.6的kill()中的0 0x4008734c
#1 0x0000b86c处于EF_中止状态(模式=0x1000)
见印刷品c:137
#在efence.c:533处的memalign(alignment=4,userSize=0)中有2个0x00009564
#3 0x0000a5bc(尺寸=0),在基准c:1027
#4个0x400fe5bc在re_node_set_alloc中(set=0x4025cfd8,size=0)
在regex_internal.c:959
#5 0x400ff2ac处于寄存器_状态(dfa=0x25,newstate=0x4025cfc8,哈希=86528)
在regex_internal.c:1550
#6重新获取状态上下文中的0x40102d64(err=0xbebd7b88,dfa=0x40196f74,
regex_internal.c:1706处的节点=0xbebd7b74,上下文=0)
#重新编译内部(preg=0xbebd7bf0,
模式=0xcb74“^([0-9]+[.]?[0-9]*.[0-9]*[.][0-9]+)[eE][+-]?[0-9]+$”,
regcomp.c:989处的长度=,语法=2428)
#8个0x4010d5f8英寸(preg=0xbebd7bf0,
模式=0xcb74“^([0-9]+[.]?[0-9]*.[0-9]*[.][0-9]+)[eE][+-]?[0-9]+$”,
注册公司c:480时的cflags=1)
#在efence.c:1244时,9 0x0000ae1c的提取时间(提取开始时间=1)
#10 0x0000aa1c位于efence.c:1144的efence_ctor()中
#11 0x0000c528 in uu libc_csu_init(argc=1,argv=0xbebd7de4,envp=0xbebd7dec)
在elf init.c:83
#12 0x40070fe8,位于启动主管道中(主管道=0x8c54,argc=1,
ubp_av=0xbebd7de4,初始值=0xc4d0,
---键入以继续,或键入q以退出---
fini=0xc4c0,rtld_fini=0x4000ea50,
堆栈_end=0xbebd7de4)在libc开始处。c:179
#13 0x00008bcc在_开始()
从GDB回溯中,我可以看出问题出在第4帧:


#4 0x400fe5bc在re_node_set_alloc(set=0x4025cfd8,size=0)
它在哪里进行malloc(0),但它是如何进行的以及为什么进行的?

更改您的电子围栏设置(
EF_ALLOW_malloc_0
)以忽略此设置


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

我可以更改设置,但我希望了解它为什么要执行malloc(0)@kingsmasher1:根据回溯,您需要查看regex_internal.c第959行附近的
re_node_set_alloc
,也可以查看第1550行附近的
register_state
。不确定您提供的代码来自何处,但它似乎并不相关。有合法的理由执行malloc(0).See.@BenVoigt:我看到了,它调用了
884 re\u node\u set\u alloc(set,size)
,大小为0,不知道为什么,而且它似乎在某些地方调用了,而在其他地方没有调用。@BenVoigt:文件是regex\u internal.c
(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 ()