c/9857: string tokenizer causes segmentation faults

ocmbah@unity.ncsu.edu ocmbah@unity.ncsu.edu
Wed Feb 26 05:46:00 GMT 2003


>Number:         9857
>Category:       c
>Synopsis:       string tokenizer causes segmentation faults
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Feb 26 05:46:01 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     ocmbah@unity.ncsu.edu
>Release:        gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)
>Organization:
>Environment:
red hate 7.3
>Description:
i noticed some strange behavior with using the strtok() function, it worked normally into my program until i got the bright idea of making a function out of some duplicate code and it mysteriously caused strtok to seg fault even though the functions that i made don't call it

i tried to reproduce this on a smaller scale w/o sending you my program so here's what i came up with, i didn't think this was going to seg fault at all but it did so...

#include <stdio.h>
#include <string.h>

#define DELIMS " \t\n"

typedef struct
{
	char   uin[9];			/* User ID */
	char   last[20];		/* Last name */
	char   first[15];		/* First name */
	int    grades[6];		/* List of student's 6 grades */
	float  average;			/* Student's average */
} studentRecord;

int main(void)
{
	int i;
	studentRecord *stuRec;
	char *string = "jdoe\tDoe\tJohn\t98\t90\t87\t88\t86\t82\t88.5";

	stuRec = (studentRecord *) malloc(sizeof(studentRecord));

	strcpy(stuRec->uin,	strtok(string, DELIMS));
	strcpy(stuRec->last,	strtok(NULL, DELIMS));
	strcpy(stuRec->first,	strtok(NULL, DELIMS));

	for(i = 0; i < 6; ++i)
		stuRec->grades[i] = atoi(strtok(NULL, DELIMS));

	stuRec->average	= atof(strtok(NULL, DELIMS));
}
>How-To-Repeat:
just compile the above example with a "gcc filename.c" and do an "./a.out"
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list