R
Roman Mashak
Hello, All!
I suppose it's C-specific issue, so I post here.
The following test code is intended to parse CGI query string, but it makes
'segmentation fault' at 'XXX' label:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* query format */
typedef struct cgi_query_s {
char arg[10];
char value[20];
} cgi_query_t;
int main(void)
{
const char seps[] = "&="; /* separators used in query */
char *token; /* splitted piece of string */
int idx = 0;
char *str, *s;
cgi_query_t *cq = calloc(10, sizeof(cgi_query_t));
strcpy(str, "arg1=qwe&arg2=asd&arg3=zxc&arg4=bnm");
token = strtok(str, seps);
puts("after token");
while( token != NULL ) {
strcpy(cq[idx].arg, token);
token = strtok(NULL, seps);
strcpy(cq[idx++].value, token); // XXX
token = strtok(NULL, seps);
}
return 0;
}
Where is possible bug?
With best regards, Roman Mashak. E-mail: (e-mail address removed)
I suppose it's C-specific issue, so I post here.
The following test code is intended to parse CGI query string, but it makes
'segmentation fault' at 'XXX' label:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* query format */
typedef struct cgi_query_s {
char arg[10];
char value[20];
} cgi_query_t;
int main(void)
{
const char seps[] = "&="; /* separators used in query */
char *token; /* splitted piece of string */
int idx = 0;
char *str, *s;
cgi_query_t *cq = calloc(10, sizeof(cgi_query_t));
strcpy(str, "arg1=qwe&arg2=asd&arg3=zxc&arg4=bnm");
token = strtok(str, seps);
puts("after token");
while( token != NULL ) {
strcpy(cq[idx].arg, token);
token = strtok(NULL, seps);
strcpy(cq[idx++].value, token); // XXX
token = strtok(NULL, seps);
}
return 0;
}
Where is possible bug?
With best regards, Roman Mashak. E-mail: (e-mail address removed)