Marius Pop Marius Pop - 4 months ago 8
C Question

Why am I getting a segmentation fault on a limit less than 169?

The program below is supposed to count the words in a C file. If I define

MAXWORD
to be less than 169 I get a segmentation fault. I find this to be confusing as I never use more than 8 characters from that 'word' string I declare inside of main. Not sure where or what to look at next so any pointers (no pun intended) would be appreciated.

After I compile it with
gcc wc.c getword.c -o wc
I run
./wc < wc.c
I get a segmentation fault but only when I set
MAXWORD
less than 169.

This is the wc.c file

#include "getword.h"
#include <stdlib.h>

#define MAXWORD 169

struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};

struct tnode *addtree(struct tnode *, char *);
void printtree(struct tnode *);

int main(void) {
char word[MAXWORD];
struct tnode *root;

while (getword(word, MAXWORD) != EOF) {
printf("word: %s\n", word);
if (isalpha(word[0]))
root = addtree(root, word);
}
printtree(root);

return 0;
}

struct tnode *talloc(void);

struct tnode *addtree(struct tnode *p, char *word) {
int cond;

if (p == NULL) {
p = talloc();
p->word = strdup(word);
p->count = 1;
p->left = p->right = NULL;
} else if ((cond = strcmp(word, p->word)) == 0)
p->count++;
else if (cond < 0)
p->left = addtree(p->left, word);
else
p->right = addtree(p->right, word);
return p;
}

struct tnode *talloc(void) {
return (struct tnode *) malloc(sizeof(struct tnode));
}

void printtree(struct tnode *p) {
if (p != NULL) {
printtree(p->left);
printf("%4d %s\n", p->count, p->word);
printtree(p->right);
}
}


And here's the getword.c file:

#include "getword.h"

#define STACKMAX 100

void skip_quote(char c);
void skip_comment(char c);
void skip_line(void);

int getch(void);
void ungetch(int);

int getword(char *word, int lim) {
int c;
char *w = word;

while (isspace(c = getch()))
;
if (c != EOF)
*w++ = c;
if (c == '#') {
skip_line();
return c;
}
if (c == '"' || c == '\'') {
skip_quote(c);
return c;
}
if (c == '/' && ((c = getch()) == '*' || c == '/')) {
skip_comment(c);
return c;
}

if (!isalpha(c)) {
*w = '\0';
return c;
}

for ( ; --lim > 0; w++)
if (!isalnum(*w = getch()) && *w != '_') {
ungetch(*w);
break;
}
*w = '\0';
return word[0];
}

void skip_quote(char type) {
int prev, current;

prev = type;
current = '\0';
while ((prev == '\\' || current != type) && prev != current) {
prev = current;
current = getch();
}
}

void skip_comment(char c) {
int prev;

prev = '\0';
if (c == '/')
skip_line();
else if (c == '*')
while (prev != '*' && (c = getch()) != '/')
prev = c;
}

void skip_line(void) {
while (getch() != '\n')
;
}

int cstack[STACKMAX];
int sp = 0;

int getch(void) {
return (sp > 0) ? cstack[--sp] : getchar();
}

void ungetch(int c) {
if (sp < STACKMAX)
cstack[sp++] = c;
else
printf("error: stack is full\n");
}


This is the header getword.h header file

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

int getword(char *, int);


Why am I getting a segmentation fault here?

Answer

You invoked undefined behavior by using a value of uninitlaized variable having automatic storage duration root, which is indeterminate, in the main() function. Try initializing it before use like

struct tnode *root = NULL;