在第4行的process_query_str中,我调用splitline并返回一个包含3个元素的字符串数组,最后一个是NULL。数组是malloced,单个字符串也是如此。然后我尝试释放每个元素,然后释放数组本身(朝向第一个函数的末尾)。前两个字符串被释放但是当它释放第三个元素时,它是NULL,我在底部得到关于无效指针的错误。显然它没有设置为NULL,应该是。我尝试放置断点,但程序不会停止。
char * process_query_str(char *rq, char *cmd) {
logg("In process_query_str.\n");
int has_query = 0;
char **tokenized_args = splitline(rq, &has_query); //Tokenize the string based on '?'
char * query_str = rq;
logg("has_query:");
fprintf(stderr, "%d", has_query);
if (has_query == 1) //Check for Query String
if (query_str != NULL) {
query_str = retrieve_query_str(tokenized_args);
logg("Query String: ");
logg(query_str);
logg("\n");
char* key = "REQUEST_METHOD"; //Process REQUEST_METHOD envir variable
if (VLstore(key, cmd) == 0)
if (VLexport(key) == 0) {
logg("Successfully exported ");
logg(cmd);
logg(": ");
logg(key);
logg("\n");
key = "QUERY_STRING"; //Process QUERY_STRING envir variable
if (VLstore(key, query_str) != 1) //1 signals a problem
if (VLexport(key) != 0) //0 signals a problem
{
logg("Successfully exported ");
logg(cmd);
logg(": ");
logg(key);
logg("\n");
}
}
}
#ifdef LOGGING //Print out environment variables
VLlist();
#endif
char *resource_str = newstr(tokenized_args[0], strlen(tokenized_args[0]));
freelist(tokenized_args);
logg("resource_str=");
logg(resource_str);
logg("\n");
return resource_str;
}
char ** splitline(char *line, int*has_query)
/*
* purpose: split a line into array of white-space separated tokens
* returns: a NULL-terminated array of pointers to copies of the tokens
* or NULL if line if no tokens on the line
* action: traverse the array, locate strings, make copies
* note: strtok() could work, but we may want to add quotes later
*/
{
//char *newstr();
logg("In splitline\n");
char **args;
int spots = 0; /* spots in table */
int bufspace = 0; /* bytes in table */
int argnum = 0; /* slots used */
char *cp = line; /* pos in string */
char *start;
int len;
if (line == NULL) /* handle special case */
return NULL;
args = emalloc(BUFSIZ); /* initialize array */
bufspace = BUFSIZ;
spots = BUFSIZ / sizeof(char *);
while (*cp != '\0') {
logg("*cp=");
fprintf(stderr, "%c", *cp);
while (*cp == ' ') /* skip leading spaces */
cp++;
if (*cp == '\0') /* quit at end-o-string */
break;
/* make sure the array has room (+1 for NULL) */
if (argnum + 1 >= spots) {
args = erealloc(args, bufspace + BUFSIZ);
bufspace += BUFSIZ;
spots += (BUFSIZ / sizeof(char *));
}
/* mark start, then find end of word */
start = cp;
len = 1;
if (*cp == '?') {
logg("query reached.\n");
*has_query = 1;
}
while (*++cp != '\0' && !(is_delim(*cp,*has_query)))
len++;
args[argnum++] = newstr(start, len);
}
logg("arg[0] =");
logg(args[0]);
logg("\n");
if (argnum == 2) {
logg("arg[1] =");
logg(args[1]);
logg("\n");
}
args[argnum] = NULL;
fprintf(stderr, "last element is NULL. argnum=%d", argnum);
return args;
}
void freelist(char **list)
/*
* purpose: free the list returned by splitline
* returns: nothing
* action: free all strings in list and then free the list
*/
{
char **cp = list;
while (*cp && (*cp)) {
logg("free: ");logg(*cp);logg("\n");
free(*cp++);
}
logg("Now Free the list:");logg("\n");
free(list);
logg("Done Freeing List\n");
}
我从glibc
获得的堆栈回溯信息是:
free: /index.cgifree: key=value*** glibc detected *** ./ws: free(): invalid pointer: 0x0804efa9 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb76d3d05]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb76d7770]
./ws[0x804b36f]
./ws[0x804a1fb]
./ws[0x8049de1]
./ws[0x8049757]
./ws[0x8049660]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb767e460]
./ws[0x8049031]
======= Memory map: ========
08048000-0804d000 r-xp 00000000 00:24 8084895 /nfs/home/j/c/jcalderon/unixlin ux/wsng/ws
0804d000-0804e000 rw-p 00004000 00:24 8084895 /nfs/home/j/c/jcalderon/unixlin ux/wsng/ws
0804e000-0806f000 rw-p 0804e000 00:00 0 [heap]
b7500000-b7521000 rw-p b7500000 00:00 0
b7521000-b7600000 ---p b7521000 00:00 0
b7644000-b764e000 r-xp 00000000 68:07 1122448 /lib/libgcc_s.so.1
b764e000-b764f000 rw-p 0000a000 68:07 1122448 /lib/libgcc_s.so.1
b765c000-b7665000 r-xp 00000000 68:07 1122335 /lib/tls/i686/cmov/libnss_files -2.7.so
b7665000-b7667000 rw-p 00008000 68:07 1122335 /lib/tls/i686/cmov/libnss_files -2.7.so
b7667000-b7668000 rw-p b7667000 00:00 0
b7668000-b77b2000 r-xp 00000000 68:07 1122611 /lib/tls/i686/cmov/libc-2.7.so
b77b2000-b77b3000 r--p 0014a000 68:07 1122611 /lib/tls/i686/cmov/libc-2.7.so
b77b3000-b77b5000 rw-p 0014b000 68:07 1122611 /lib/tls/i686/cmov/libc-2.7.so
b77b5000-b77b8000 rw-p b77b5000 00:00 0
b77c2000-b77c7000 rw-p b77c2000 00:00 0
b77c7000-b77c8000 r-xp b77c7000 00:00 0 [vdso]
b77c8000-b77e2000 r-xp 00000000 68:07 1124008 /lib/ld-2.7.so
b77e2000-b77e4000 rw-p 00019000 68:07 1124008 /lib/ld-2.7.so
bfd9f000-bfdb4000 rw-p bffea000 00:00 0 [stack]
答案 0 :(得分:2)
这不是您问题的答案,但它可以帮助您减少代码量。你有片段,如:
logg("Successfully exported ");
logg(cmd);
logg(": ");
logg(key);
logg("\n");
如果您能写下来,这会更简单:
logg("Successfully exported %s: %s\n", cmd, key);
你可以通过改编这个功能来做到这一点:
#include <stdarg.h>
#include <stdio.h>
void logg(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
您可以在标题中声明:
#ifdef __GNUC__
#define PRINTFLIKE(n,m) __attribute__((format(printf,n,m)))
#else
#define PRINTFLIKE(n,m) /* If only */
#endif /* __GNUC__ */
extern void logg(const char *format, ...) PRINTFLIKE(1, 2);
然后GCC会发现格式转换规范的误用与传递给函数的参数有关。
请注意,此版本的logg()
基本上与前一版本调用兼容。唯一出现差异的是,如果打印的字符串包含%Z
- 类似的字符串,现在将其视为转换规范,而不是之前的字符串。但是,您应该使用logg("%s\n", argument);
或类似的。
答案 1 :(得分:2)
事实证明这是一个malloc问题,我没有在newstr()中分配足够的空间。我试图释放它时,最后一个数组元素导致错误。
答案 2 :(得分:0)
我觉得这里有过度防守的编程,有些机会可以分解一些功能。
考虑一下:
char **tokenized_args = splitline(rq, &has_query);
char * query_str = rq;
logg("has_query:");
fprintf(stderr, "%d", has_query);
if (has_query == 1) //Check for Query String
if (query_str != NULL) {
query_str = retrieve_query_str(tokenized_args);
query_str
被用于两个不同的事情 - 其中一个非常短暂。您为什么要通过别名rq != NULL
检查query_str
?为什么不直接测试rq != NULL
?请注意,has_query == 1
只会是真正的IFF rq != NULL
。
has_query == 1
或query_str != NULL
中的一个多余,应该完全删除。
考虑一下:
char* key = "REQUEST_METHOD";
if (VLstore(key, cmd) == 0)
if (VLexport(key) == 0) {
logg("Successfully exported ");
logg(cmd);
logg(": ");
logg(key);
logg("\n");
此日志记录可能应包含在VLstore()
和VLexport()
函数中。您可以将这些行简化为:
if (!VLstore("REQUEST_METHOD", cmd))
/* error */
if (!VLexport("REQUEST_METHOD"))
/* error */
if (!VLstore("QUERY_STRING", cmd))
/* error */
if (!VLexport("QUERY_STRING"))
/* error */
现在,我认为代码会给你带来麻烦:
char *resource_str = newstr(tokenized_args[0], strlen(tokenized_args[0]));
freelist(tokenized_args);
如果rq != NULL
选择性地执行,它会出现在块的末尾。但是如果rq == NULL
也可以 执行,在这种情况下,tokenized_args[0]
应该是NULL
- 指针取消引用。由于freelist()
也取消引用您传递的NULL
指针,因此它也会在那里爆炸。
我认为您应该将此例程重写为两个例程 - 一个用于处理案例rq == NULL
,另一个用于处理案例rq != NULL
。如果使用rq == NULL
调用此例程是错误的,请不要在rq == NULL
的情况下尝试友好。早死。在例程顶部附近添加assert(rq)
并删除冗余检查会大大简化此代码,使您可以更轻松地阅读它。