array of structures

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)
 
R

Richard Heathfield

Roman said:
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");

str doesn't point to sufficient space for this copy to work. In fact, str
doesn't point anywhere. Did you think char * means string? It doesn't.
 
J

John Bode

Roman said:
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");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is most likely the cause of your problem. You've declared the
pointer (str), but you haven't given it anything to point *to*, so it's
pointing off into some random memory location.

You either need to declare str as a static array:

char str[] = "arg1=...";

or you need to use malloc() to create a buffer and copy the string to
it:

str = malloc(sizeof *str * strlen("arg1=...") + 1;
if (str)
strcpy(str, "arg1=...");
else
/* memory allocation error */
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)
 
G

goose

Roman said:
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:
<snipped>

In addition to the other responses:
token = strtok(NULL, seps);
strcpy(cq[idx++].value, token); // XXX

strtok may return NULL. You must test token
for non-NULL-ness before using it.
 
R

Roman Mashak

Thanks to everyone for replies and hints.
To my mind, strtok() is pretty archaism :) don't you think so?

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top