Blame SOURCES/wordnet-3.0-CVE-2008-3908.patch

f3e103
diff --git a/lib/binsrch.c b/lib/binsrch.c
f3e103
index 85436f3..8b71216 100644
f3e103
--- a/lib/binsrch.c
f3e103
+++ b/lib/binsrch.c
f3e103
@@ -28,7 +28,7 @@ char *read_index(long offset, FILE *fp) {
f3e103
     char *linep;
f3e103
 
f3e103
     linep = line;
f3e103
-    line[0] = '0';
f3e103
+    line[0] = '\0';
f3e103
 
f3e103
     fseek( fp, offset, SEEK_SET );
f3e103
     fgets(linep, LINE_LEN, fp);
f3e103
@@ -58,6 +58,8 @@ char *bin_search(char *searchkey, FILE *fp)
f3e103
         last_bin_search_offset = ftell( fp );
f3e103
 	fgets(linep, LINE_LEN, fp);
f3e103
 	length = (int)(strchr(linep, ' ') - linep);
f3e103
+	if (length > (sizeof(key) - 1))
f3e103
+            return(NULL);
f3e103
 	strncpy(key, linep, length);
f3e103
 	key[length] = '\0';
f3e103
 	if(strcmp(key, searchkey) < 0) {
f3e103
@@ -110,6 +112,8 @@ static int bin_search_key(char *searchkey, FILE *fp)
f3e103
 	line[length++] =  c;
f3e103
     if (getc(fp) == EOF) {	/* only 1 line in file */
f3e103
 	length = (int)(strchr(linep, ' ') - linep);
f3e103
+	if (length > (sizeof(key) - 1))
f3e103
+            return(0);
f3e103
 	strncpy(key, linep, length);
f3e103
 	key[length] = '\0';
f3e103
 	if(strcmp(key, searchkey) > 0) {
f3e103
@@ -132,6 +136,8 @@ static int bin_search_key(char *searchkey, FILE *fp)
f3e103
 	if (fgets(linep, LINE_LEN, fp) != NULL) {
f3e103
   	    offset2 = ftell(fp); /* offset at start of next line */
f3e103
 	    length = (int)(strchr(linep, ' ') - linep);
f3e103
+	    if (length > (sizeof(key) - 1))
f3e103
+                return(0);
f3e103
 	    strncpy(key, linep, length);
f3e103
 	    key[length] = '\0';
f3e103
 	    if(strcmp(key, searchkey) < 0) {	/* further in file */
f3e103
diff --git a/lib/morph.c b/lib/morph.c
f3e103
index 0cff594..ea4b4f8 100644
f3e103
--- a/lib/morph.c
f3e103
+++ b/lib/morph.c
f3e103
@@ -51,24 +51,24 @@ static struct {
f3e103
     char *str;
f3e103
     int strlen;
f3e103
 } prepositions[NUMPREPS] = {
f3e103
-    "to", 2,
f3e103
-    "at", 2,
f3e103
-    "of", 2,
f3e103
-    "on", 2,
f3e103
-    "off", 3,
f3e103
-    "in", 2,
f3e103
-    "out", 3,
f3e103
-    "up", 2,
f3e103
-    "down", 4,
f3e103
-    "from", 4,
f3e103
-    "with", 4,
f3e103
-    "into", 4,
f3e103
-    "for", 3,
f3e103
-    "about", 5,
f3e103
-    "between", 7,
f3e103
+    { "to", 2 },
f3e103
+    { "at", 2 },
f3e103
+    { "of", 2 },
f3e103
+    { "on", 2 },
f3e103
+    { "off", 3 },
f3e103
+    { "in", 2 },
f3e103
+    { "out", 3 },
f3e103
+    { "up", 2 },
f3e103
+    { "down", 4 },
f3e103
+    { "from", 4 },
f3e103
+    { "with", 4 },
f3e103
+    { "into", 4 },
f3e103
+    { "for", 3 },
f3e103
+    { "about", 5 },
f3e103
+    { "between", 7 }
f3e103
 };
f3e103
 
f3e103
-static FILE *exc_fps[NUMPARTS + 1];
f3e103
+static FILE *exc_fps[NUMPARTS];
f3e103
 
f3e103
 static int do_init();
f3e103
 static int strend(char *, char *);
f3e103
@@ -100,7 +100,7 @@ int re_morphinit(void)
f3e103
 {
f3e103
     int i;
f3e103
 
f3e103
-    for (i = 1; i <= NUMPARTS; i++) {
f3e103
+    for (i = 0; i < NUMPARTS; i++) {
f3e103
 	if (exc_fps[i] != NULL) {
f3e103
 	    fclose(exc_fps[i]); exc_fps[i] = NULL;
f3e103
 	}
f3e103
@@ -144,18 +144,19 @@ static int do_init(void)
f3e103
     } else
f3e103
 	sprintf(searchdir, DEFAULTPATH);
f3e103
 #else
f3e103
-    if ((env = getenv("WNSEARCHDIR")) != NULL)
f3e103
-	strcpy(searchdir, env);
f3e103
-    else if ((env = getenv("WNHOME")) != NULL)
f3e103
-	sprintf(searchdir, "%s%s", env, DICTDIR);
f3e103
-    else
f3e103
+    if ((env = getenv("WNSEARCHDIR")) != NULL) {
f3e103
+	snprintf(searchdir, sizeof(searchdir), "%s", env);
f3e103
+    } else if ((env = getenv("WNHOME")) != NULL) {
f3e103
+	snprintf(searchdir, sizeof(searchdir), "%s%s", env, DICTDIR);
f3e103
+    } else {
f3e103
 	strcpy(searchdir, DEFAULTPATH);
f3e103
+    }
f3e103
 #endif
f3e103
 
f3e103
-    for (i = 1; i <= NUMPARTS; i++) {
f3e103
-	sprintf(fname, EXCFILE, searchdir, partnames[i]);
f3e103
+    for (i = 0; i < NUMPARTS; i++) {
f3e103
+	snprintf(fname, sizeof(fname), EXCFILE, searchdir, partnames[i+1]);
f3e103
 	if ((exc_fps[i] = fopen(fname, "r")) == NULL) {
f3e103
-	    sprintf(msgbuf,
f3e103
+	    snprintf(msgbuf, sizeof(msgbuf),
f3e103
 		    "WordNet library error: Can't open exception file(%s)\n\n",
f3e103
 		    fname);
f3e103
 	    display_message(msgbuf);
f3e103
@@ -178,13 +179,16 @@ char *morphstr(char *origstr, int pos)
f3e103
     int prep;
f3e103
     char *end_idx1, *end_idx2;
f3e103
     char *append;
f3e103
-    
f3e103
+
f3e103
     if (pos == SATELLITE)
f3e103
 	pos = ADJ;
f3e103
 
f3e103
     /* First time through for this string */
f3e103
 
f3e103
     if (origstr != NULL) {
f3e103
+        if (strlen(origstr) > WORDBUF - 1)
f3e103
+            return(NULL);
f3e103
+
f3e103
 	/* Assume string hasn't had spaces substitued with '_' */
f3e103
 	strtolower(strsubst(strcpy(str, origstr), ' ', '_'));
f3e103
 	searchstr[0] = '\0';
f3e103
@@ -232,7 +236,7 @@ char *morphstr(char *origstr, int pos)
f3e103
 		if (end_idx < 0) return(NULL);		/* shouldn't do this */
f3e103
 		strncpy(word, str + st_idx, end_idx - st_idx);
f3e103
 		word[end_idx - st_idx] = '\0';
f3e103
-		if(tmp = morphword(word, pos))
f3e103
+		if ((tmp = morphword(word, pos)) != NULL)
f3e103
 		    strcat(searchstr,tmp);
f3e103
 		else
f3e103
 		    strcat(searchstr,word);
f3e103
@@ -240,7 +244,7 @@ char *morphstr(char *origstr, int pos)
f3e103
 		st_idx = end_idx + 1;
f3e103
 	    }
f3e103
 	    
f3e103
-	    if(tmp = morphword(strcpy(word, str + st_idx), pos)) 
f3e103
+	    if ((tmp = morphword(strcpy(word, str + st_idx), pos)) != NULL)
f3e103
 		strcat(searchstr,tmp);
f3e103
 	    else
f3e103
 		strcat(searchstr,word);
f3e103
@@ -270,16 +274,15 @@ char *morphword(char *word, int pos)
f3e103
 {
f3e103
     int offset, cnt;
f3e103
     int i;
f3e103
-    static char retval[WORDBUF];
f3e103
-    char *tmp, tmpbuf[WORDBUF], *end;
f3e103
-    
f3e103
-    sprintf(retval,"");
f3e103
-    sprintf(tmpbuf, "");
f3e103
-    end = "";
f3e103
-    
f3e103
+    static char retval[WORDBUF] = "";
f3e103
+    char *tmp, tmpbuf[WORDBUF] = "", *end = "";
f3e103
+
f3e103
     if(word == NULL) 
f3e103
 	return(NULL);
f3e103
 
f3e103
+    if (strlen(word) > WORDBUF - 1)
f3e103
+        return(NULL);
f3e103
+
f3e103
     /* first look for word on exception list */
f3e103
     
f3e103
     if((tmp = exc_lookup(word, pos)) != NULL)
f3e103
@@ -335,7 +338,10 @@ static char *wordbase(char *word, int ender)
f3e103
 {
f3e103
     char *pt1;
f3e103
     static char copy[WORDBUF];
f3e103
-    
f3e103
+
f3e103
+    if (strlen(word) > WORDBUF - 1)
f3e103
+        return(NULL);
f3e103
+
f3e103
     strcpy(copy, word);
f3e103
     if(strend(copy,sufx[ender])) {
f3e103
 	pt1=strchr(copy,'\0');
f3e103
@@ -368,13 +374,14 @@ static char *exc_lookup(char *word, int pos)
f3e103
 {
f3e103
     static char line[WORDBUF], *beglp, *endlp;
f3e103
     char *excline;
f3e103
-    int found = 0;
f3e103
 
f3e103
     if (exc_fps[pos] == NULL)
f3e103
 	return(NULL);
f3e103
 
f3e103
     /* first time through load line from exception file */
f3e103
     if(word != NULL){
f3e103
+        if (strlen(word) > WORDBUF - 1)
f3e103
+           return(NULL);
f3e103
 	if ((excline = bin_search(word, exc_fps[pos])) != NULL) {
f3e103
 	    strcpy(line, excline);
f3e103
 	    endlp = strchr(line,' ');
f3e103
@@ -403,6 +410,9 @@ static char *morphprep(char *s)
f3e103
     char word[WORDBUF], end[WORDBUF];
f3e103
     static char retval[WORDBUF];
f3e103
 
f3e103
+    if (strlen(s) > WORDBUF - 1)
f3e103
+        return (NULL);
f3e103
+
f3e103
     /* Assume that the verb is the first word in the phrase.  Strip it
f3e103
        off, check for validity, then try various morphs with the
f3e103
        rest of the phrase tacked on, trying to find a match. */
f3e103
@@ -410,7 +420,7 @@ static char *morphprep(char *s)
f3e103
     rest = strchr(s, '_');
f3e103
     last = strrchr(s, '_');
f3e103
     if (rest != last) {		/* more than 2 words */
f3e103
-	if (lastwd = morphword(last + 1, NOUN)) {
f3e103
+	if ((lastwd = morphword(last + 1, NOUN)) != NULL) {
f3e103
 	    strncpy(end, rest, last - rest + 1);
f3e103
 	    end[last-rest+1] = '\0';
f3e103
 	    strcat(end, lastwd);
f3e103
diff --git a/lib/search.c b/lib/search.c
f3e103
index 1cdedc3..bc781cd 100644
f3e103
--- a/lib/search.c
f3e103
+++ b/lib/search.c
f3e103
@@ -13,6 +13,7 @@
f3e103
 #include <stdlib.h>
f3e103
 #include <string.h>
f3e103
 #include <assert.h>
f3e103
+#include <limits.h>
f3e103
 
f3e103
 #include "wn.h"
f3e103
 
f3e103
@@ -119,33 +120,22 @@ IndexPtr parse_index(long offset, int dbase, char *line) {
f3e103
     if ( !line )
f3e103
       line = read_index( offset, indexfps[dbase] );
f3e103
     
f3e103
-    idx = (IndexPtr)malloc(sizeof(Index));
f3e103
+    idx = (IndexPtr)calloc(1, sizeof(Index));
f3e103
     assert(idx);
f3e103
 
f3e103
     /* set offset of entry in index file */
f3e103
     idx->idxoffset = offset;
f3e103
     
f3e103
-    idx->wd='\0';
f3e103
-    idx->pos='\0';
f3e103
-    idx->off_cnt=0;
f3e103
-    idx->tagged_cnt = 0;
f3e103
-    idx->sense_cnt=0;
f3e103
-    idx->offset='\0';
f3e103
-    idx->ptruse_cnt=0;
f3e103
-    idx->ptruse='\0';
f3e103
-    
f3e103
     /* get the word */
f3e103
     ptrtok=strtok(line," \n");
f3e103
     
f3e103
-    idx->wd = malloc(strlen(ptrtok) + 1);
f3e103
+    idx->wd = strdup(ptrtok);
f3e103
     assert(idx->wd);
f3e103
-    strcpy(idx->wd, ptrtok);
f3e103
     
f3e103
     /* get the part of speech */
f3e103
     ptrtok=strtok(NULL," \n");
f3e103
-    idx->pos = malloc(strlen(ptrtok) + 1);
f3e103
+    idx->pos = strdup(ptrtok);
f3e103
     assert(idx->pos);
f3e103
-    strcpy(idx->pos, ptrtok);
f3e103
     
f3e103
     /* get the collins count */
f3e103
     ptrtok=strtok(NULL," \n");
f3e103
@@ -154,7 +144,12 @@ IndexPtr parse_index(long offset, int dbase, char *line) {
f3e103
     /* get the number of pointers types */
f3e103
     ptrtok=strtok(NULL," \n");
f3e103
     idx->ptruse_cnt = atoi(ptrtok);
f3e103
-    
f3e103
+
f3e103
+    if (idx->ptruse_cnt < 0 || (unsigned int)idx->ptruse_cnt > UINT_MAX/sizeof(int)) {
f3e103
+        free_index(idx);
f3e103
+        return(NULL);
f3e103
+    }
f3e103
+
f3e103
     if (idx->ptruse_cnt) {
f3e103
 	idx->ptruse = (int *) malloc(idx->ptruse_cnt * (sizeof(int)));
f3e103
 	assert(idx->ptruse);
f3e103
@@ -173,9 +168,14 @@ IndexPtr parse_index(long offset, int dbase, char *line) {
f3e103
     /* get the number of senses that are tagged */
f3e103
     ptrtok=strtok(NULL," \n");
f3e103
     idx->tagged_cnt = atoi(ptrtok);
f3e103
-        
f3e103
+
f3e103
+    if (idx->off_cnt < 0 || (unsigned long)idx->off_cnt > ULONG_MAX/sizeof(long)) {
f3e103
+        free_index(idx);
f3e103
+        return(NULL);
f3e103
+    }
f3e103
+
f3e103
     /* make space for the offsets */
f3e103
-    idx->offset = (long *) malloc(idx->off_cnt * (sizeof(long)));
f3e103
+    idx->offset = (unsigned long *) malloc(idx->off_cnt * sizeof(long));
f3e103
     assert(idx->offset);
f3e103
     
f3e103
     /* get the offsets */
f3e103
@@ -197,15 +197,21 @@ IndexPtr getindex(char *searchstr, int dbase)
f3e103
     char strings[MAX_FORMS][WORDBUF]; /* vector of search strings */
f3e103
     static IndexPtr offsets[MAX_FORMS];
f3e103
     static int offset;
f3e103
-    
f3e103
+
f3e103
     /* This works like strrok(): if passed with a non-null string,
f3e103
        prepare vector of search strings and offsets.  If string
f3e103
        is null, look at current list of offsets and return next
f3e103
        one, or NULL if no more alternatives for this word. */
f3e103
 
f3e103
     if (searchstr != NULL) {
f3e103
-
f3e103
-	offset = 0;
f3e103
+        /* Bail out if the input is too long for us to handle */
f3e103
+        if (strlen(searchstr) > (WORDBUF - 1)) {
f3e103
+            strcpy(msgbuf, "WordNet library error: search term is too long\n");
f3e103
+                   display_message(msgbuf);
f3e103
+            return(NULL);
f3e103
+        }
f3e103
+
f3e103
+    	offset = 0;
f3e103
 	strtolower(searchstr);
f3e103
 	for (i = 0; i < MAX_FORMS; i++) {
f3e103
 	    strcpy(strings[i], searchstr);
f3e103
@@ -229,11 +235,11 @@ IndexPtr getindex(char *searchstr, int dbase)
f3e103
 	/* Get offset of first entry.  Then eliminate duplicates
f3e103
 	   and get offsets of unique strings. */
f3e103
 
f3e103
-	if (strings[0][0] != NULL)
f3e103
+	if (strings[0] != NULL)
f3e103
 	    offsets[0] = index_lookup(strings[0], dbase);
f3e103
 
f3e103
 	for (i = 1; i < MAX_FORMS; i++)
f3e103
-	    if ((strings[i][0]) != NULL && (strcmp(strings[0], strings[i])))
f3e103
+	    if (strings[i] != NULL && (strcmp(strings[0], strings[i])))
f3e103
 		offsets[i] = index_lookup(strings[i], dbase);
f3e103
     }
f3e103
 
f3e103
@@ -272,7 +278,7 @@ SynsetPtr read_synset(int dbase, long boffset, char *word)
f3e103
 SynsetPtr parse_synset(FILE *fp, int dbase, char *word)
f3e103
 {
f3e103
     static char line[LINEBUF];
f3e103
-    char tbuf[SMLINEBUF];
f3e103
+    char tbuf[SMLINEBUF] = "";
f3e103
     char *ptrtok;
f3e103
     char *tmpptr;
f3e103
     int foundpert = 0;
f3e103
@@ -286,33 +292,11 @@ SynsetPtr parse_synset(FILE *fp, int dbase, char *word)
f3e103
     if ((tmpptr = fgets(line, LINEBUF, fp)) == NULL)
f3e103
 	return(NULL);
f3e103
     
f3e103
-    synptr = (SynsetPtr)malloc(sizeof(Synset));
f3e103
+    synptr = (SynsetPtr)calloc(1, sizeof(Synset));
f3e103
     assert(synptr);
f3e103
-    
f3e103
-    synptr->hereiam = 0;
f3e103
+
f3e103
     synptr->sstype = DONT_KNOW;
f3e103
-    synptr->fnum = 0;
f3e103
-    synptr->pos = '\0';
f3e103
-    synptr->wcount = 0;
f3e103
-    synptr->words = '\0';
f3e103
-    synptr->whichword = 0;
f3e103
-    synptr->ptrcount = 0;
f3e103
-    synptr->ptrtyp = '\0';
f3e103
-    synptr->ptroff = '\0';
f3e103
-    synptr->ppos = '\0';
f3e103
-    synptr->pto = '\0';
f3e103
-    synptr->pfrm = '\0';
f3e103
-    synptr->fcount = 0;
f3e103
-    synptr->frmid = '\0';
f3e103
-    synptr->frmto = '\0';
f3e103
-    synptr->defn = '\0';
f3e103
-    synptr->key = 0;
f3e103
-    synptr->nextss = NULL;
f3e103
-    synptr->nextform = NULL;
f3e103
     synptr->searchtype = -1;
f3e103
-    synptr->ptrlist = NULL;
f3e103
-    synptr->headword = NULL;
f3e103
-    synptr->headsense = 0;
f3e103
 
f3e103
     ptrtok = line;
f3e103
     
f3e103
@@ -322,7 +306,7 @@ SynsetPtr parse_synset(FILE *fp, int dbase, char *word)
f3e103
 
f3e103
     /* sanity check - make sure starting file offset matches first field */
f3e103
     if (synptr->hereiam != loc) {
f3e103
-	sprintf(msgbuf, "WordNet library error: no synset at location %d\n",
f3e103
+	sprintf(msgbuf, "WordNet library error: no synset at location %ld\n",
f3e103
 		loc);
f3e103
 	display_message(msgbuf);
f3e103
 	free(synptr);
f3e103
@@ -335,16 +319,20 @@ SynsetPtr parse_synset(FILE *fp, int dbase, char *word)
f3e103
     
f3e103
     /* looking at POS */
f3e103
     ptrtok = strtok(NULL, " \n");
f3e103
-    synptr->pos = malloc(strlen(ptrtok) + 1);
f3e103
+    synptr->pos = strdup(ptrtok);
f3e103
     assert(synptr->pos);
f3e103
-    strcpy(synptr->pos, ptrtok);
f3e103
     if (getsstype(synptr->pos) == SATELLITE)
f3e103
 	synptr->sstype = INDIRECT_ANT;
f3e103
     
f3e103
     /* looking at numwords */
f3e103
     ptrtok = strtok(NULL, " \n");
f3e103
     synptr->wcount = strtol(ptrtok, NULL, 16);
f3e103
-    
f3e103
+
f3e103
+    if (synptr->wcount < 0 || (unsigned int)synptr->wcount > UINT_MAX/sizeof(char *)) {
f3e103
+        free_syns(synptr);
f3e103
+        return(NULL);
f3e103
+    }
f3e103
+
f3e103
     synptr->words = (char **)malloc(synptr->wcount  * sizeof(char *));
f3e103
     assert(synptr->words);
f3e103
     synptr->wnsns = (int *)malloc(synptr->wcount * sizeof(int));
f3e103
@@ -354,9 +342,8 @@ SynsetPtr parse_synset(FILE *fp, int dbase, char *word)
f3e103
     
f3e103
     for (i = 0; i < synptr->wcount; i++) {
f3e103
 	ptrtok = strtok(NULL, " \n");
f3e103
-	synptr->words[i] = malloc(strlen(ptrtok) + 1);
f3e103
+	synptr->words[i] = strdup(ptrtok);
f3e103
 	assert(synptr->words[i]);
f3e103
-	strcpy(synptr->words[i], ptrtok);
f3e103
 	
f3e103
 	/* is this the word we're looking for? */
f3e103
 	
f3e103
@@ -371,6 +358,12 @@ SynsetPtr parse_synset(FILE *fp, int dbase, char *word)
f3e103
     ptrtok = strtok(NULL," \n");
f3e103
     synptr->ptrcount = atoi(ptrtok);
f3e103
 
f3e103
+    /* Should we check for long here as well? */
f3e103
+    if (synptr->ptrcount < 0 || (unsigned int)synptr->ptrcount > UINT_MAX/sizeof(int)) {
f3e103
+        free_syns(synptr);
f3e103
+        return(NULL);
f3e103
+    }
f3e103
+
f3e103
     if (synptr->ptrcount) {
f3e103
 
f3e103
 	/* alloc storage for the pointers */
f3e103
@@ -455,21 +448,23 @@ SynsetPtr parse_synset(FILE *fp, int dbase, char *word)
f3e103
     ptrtok = strtok(NULL," \n");
f3e103
     if (ptrtok) {
f3e103
 	ptrtok = strtok(NULL," \n");
f3e103
-	sprintf(tbuf, "");
f3e103
 	while (ptrtok != NULL) {
f3e103
+	    if (strlen(ptrtok) + strlen(tbuf) + 1 + 1 > sizeof(tbuf)) {
f3e103
+                free_syns(synptr);
f3e103
+                return(NULL);
f3e103
+	    }
f3e103
 	    strcat(tbuf,ptrtok);
f3e103
 	    ptrtok = strtok(NULL, " \n");
f3e103
 	    if(ptrtok)
f3e103
 		strcat(tbuf," ");
f3e103
 	}
f3e103
-	assert((1 + strlen(tbuf)) < sizeof(tbuf));
f3e103
-	synptr->defn = malloc(strlen(tbuf) + 4);
f3e103
+	synptr->defn = malloc(strlen(tbuf) + 3);
f3e103
 	assert(synptr->defn);
f3e103
 	sprintf(synptr->defn,"(%s)",tbuf);
f3e103
     }
f3e103
 
f3e103
     if (keyindexfp) { 		/* we have unique keys */
f3e103
-	sprintf(tmpbuf, "%c:%8.8d", partchars[dbase], synptr->hereiam);
f3e103
+	sprintf(tmpbuf, "%c:%8.8ld", partchars[dbase], synptr->hereiam);
f3e103
 	synptr->key = GetKeyForOffset(tmpbuf);
f3e103
     }
f3e103
 
f3e103
@@ -635,7 +630,7 @@ static void traceptrs(SynsetPtr synptr, int ptrtyp, int dbase, int depth)
f3e103
 
f3e103
 	    if ((ptrtyp == PERTPTR || ptrtyp == PPLPTR) &&
f3e103
 		synptr->pto[i] != 0) {
f3e103
-		sprintf(tbuf, " (Sense %d)\n",
f3e103
+		snprintf(tbuf, sizeof(tbuf), " (Sense %d)\n",
f3e103
 			cursyn->wnsns[synptr->pto[i] - 1]);
f3e103
 		printsynset(prefix, cursyn, tbuf, DEFOFF, synptr->pto[i],
f3e103
 			    SKIP_ANTS, PRINT_MARKER);
f3e103
@@ -656,7 +651,7 @@ static void traceptrs(SynsetPtr synptr, int ptrtyp, int dbase, int depth)
f3e103
 		    traceptrs(cursyn, HYPERPTR, getpos(cursyn->pos), 0);
f3e103
 		}
f3e103
 	    } else if (ptrtyp == ANTPTR && dbase != ADJ && synptr->pto[i] != 0) {
f3e103
-		sprintf(tbuf, " (Sense %d)\n",
f3e103
+		snprintf(tbuf, sizeof(tbuf), " (Sense %d)\n",
f3e103
 			cursyn->wnsns[synptr->pto[i] - 1]);
f3e103
 		printsynset(prefix, cursyn, tbuf, DEFOFF, synptr->pto[i],
f3e103
 			    SKIP_ANTS, PRINT_MARKER);
f3e103
@@ -817,7 +812,7 @@ static void tracenomins(SynsetPtr synptr, int dbase)
f3e103
 	    	    
f3e103
 	    cursyn = read_synset(synptr->ppos[i], synptr->ptroff[i], "");
f3e103
 
f3e103
-	    sprintf(tbuf, "#%d\n",
f3e103
+	    snprintf(tbuf, sizeof(tbuf), "#%d\n",
f3e103
 		    cursyn->wnsns[synptr->pto[i] - 1]);
f3e103
 	    printsynset(prefix, cursyn, tbuf, DEFOFF, synptr->pto[i],
f3e103
 			SKIP_ANTS, SKIP_MARKER);
f3e103
@@ -989,12 +984,12 @@ void getexample(char *offset, char *wd)
f3e103
     char sentbuf[512];
f3e103
     
f3e103
     if (vsentfilefp != NULL) {
f3e103
-	if (line = bin_search(offset, vsentfilefp)) {
f3e103
+	if ((line = bin_search(offset, vsentfilefp)) != NULL) {
f3e103
 	    while(*line != ' ') 
f3e103
 		line++;
f3e103
 
f3e103
 	    printbuffer("          EX: ");
f3e103
-	    sprintf(sentbuf, line, wd);
f3e103
+	    snprintf(sentbuf, sizeof(sentbuf), line, wd);
f3e103
 	    printbuffer(sentbuf);
f3e103
 	}
f3e103
     }
f3e103
@@ -1011,7 +1006,7 @@ int findexample(SynsetPtr synptr)
f3e103
     if (vidxfilefp != NULL) {
f3e103
 	wdnum = synptr->whichword - 1;
f3e103
 
f3e103
-	sprintf(tbuf,"%s%%%-1.1d:%-2.2d:%-2.2d::",
f3e103
+	snprintf(tbuf, sizeof(tbuf), "%s%%%-1.1d:%-2.2d:%-2.2d::",
f3e103
 		synptr->words[wdnum],
f3e103
 		getpos(synptr->pos),
f3e103
 		synptr->fnum,
f3e103
@@ -1124,7 +1119,7 @@ static void freq_word(IndexPtr index)
f3e103
 	if (cnt >= 17 && cnt <= 32) familiar = 6;
f3e103
 	if (cnt > 32 ) familiar = 7;
f3e103
 	
f3e103
-	sprintf(tmpbuf,
f3e103
+	snprintf(tmpbuf, sizeof(tmpbuf),
f3e103
 		"\n%s used as %s is %s (polysemy count = %d)\n",
f3e103
 		index->wd, a_an[getpos(index->pos)], freqcats[familiar], cnt);
f3e103
 	printbuffer(tmpbuf);
f3e103
@@ -1147,6 +1142,9 @@ void wngrep (char *word_passed, int pos) {
f3e103
    }
f3e103
    rewind(inputfile);
f3e103
 
f3e103
+   if (strlen(word_passed) + 1 > sizeof(word))
f3e103
+       return;
f3e103
+
f3e103
    strcpy (word, word_passed);
f3e103
    ToLowerCase(word);		/* map to lower case for index file search */
f3e103
    strsubst (word, ' ', '_');	/* replace spaces with underscores */
f3e103
@@ -1169,7 +1167,7 @@ void wngrep (char *word_passed, int pos) {
f3e103
             ((line[loc + wordlen] == '-') || (line[loc + wordlen] == '_')))
f3e103
          ) {
f3e103
             strsubst (line, '_', ' ');
f3e103
-            sprintf (tmpbuf, "%s\n", line);
f3e103
+            snprintf (tmpbuf, sizeof(tmpbuf), "%s\n", line);
f3e103
             printbuffer (tmpbuf);
f3e103
             break;
f3e103
          }
f3e103
@@ -1570,7 +1568,8 @@ char *findtheinfo(char *searchstr, int dbase, int ptrtyp, int whichsense)
f3e103
 			bufstart[0] = '\n';
f3e103
 			bufstart++;
f3e103
 		    }
f3e103
-		    strncpy(bufstart, tmpbuf, strlen(tmpbuf));
f3e103
+		    /* Don't include the \0 */
f3e103
+		    memcpy(bufstart, tmpbuf, strlen(tmpbuf));
f3e103
 		    bufstart = searchbuffer + strlen(searchbuffer);
f3e103
 		}
f3e103
 	    }
f3e103
@@ -1683,9 +1682,8 @@ SynsetPtr traceptrs_ds(SynsetPtr synptr, int ptrtyp, int dbase, int depth)
f3e103
 		cursyn = read_synset(synptr->ppos[i],
f3e103
 				      synptr->ptroff[i],
f3e103
 				      "");
f3e103
-		synptr->headword = malloc(strlen(cursyn->words[0]) + 1);
f3e103
+		synptr->headword = strdup(cursyn->words[0]);
f3e103
 		assert(synptr->headword);
f3e103
-		strcpy(synptr->headword, cursyn->words[0]);
f3e103
 		synptr->headsense = cursyn->lexid[0];
f3e103
 		free_synset(cursyn);
f3e103
 		break;
f3e103
@@ -2013,7 +2011,7 @@ static int getsearchsense(SynsetPtr synptr, int whichword)
f3e103
     strsubst(strcpy(wdbuf, synptr->words[whichword - 1]), ' ', '_');
f3e103
     strtolower(wdbuf);
f3e103
 		       
f3e103
-    if (idx = index_lookup(wdbuf, getpos(synptr->pos))) {
f3e103
+    if ((idx = index_lookup(wdbuf, getpos(synptr->pos))) != NULL) {
f3e103
 	for (i = 0; i < idx->off_cnt; i++)
f3e103
 	    if (idx->offset[i] == synptr->hereiam) {
f3e103
 		free_index(idx);
f3e103
@@ -2037,7 +2035,7 @@ static void printsynset(char *head, SynsetPtr synptr, char *tail, int definition
f3e103
        by flags */
f3e103
 
f3e103
     if (offsetflag)		/* print synset offset */
f3e103
-	sprintf(tbuf + strlen(tbuf),"{%8.8d} ", synptr->hereiam);
f3e103
+	sprintf(tbuf + strlen(tbuf),"{%8.8ld} ", synptr->hereiam);
f3e103
     if (fileinfoflag) {		/* print lexicographer file information */
f3e103
 	sprintf(tbuf + strlen(tbuf), "<%s> ", lexfiles[synptr->fnum]);
f3e103
 	prlexid = 1;		/* print lexicographer id after word */
f3e103
@@ -2072,7 +2070,7 @@ static void printantsynset(SynsetPtr synptr, char *tail, int anttype, int defini
f3e103
     tbuf[0] = '\0';
f3e103
 
f3e103
     if (offsetflag)
f3e103
-	sprintf(tbuf,"{%8.8d} ", synptr->hereiam);
f3e103
+	sprintf(tbuf,"{%8.8ld} ", synptr->hereiam);
f3e103
     if (fileinfoflag) {
f3e103
 	sprintf(tbuf + strlen(tbuf),"<%s> ", lexfiles[synptr->fnum]);
f3e103
 	prlexid = 1;
f3e103
diff --git a/lib/wnutil.c b/lib/wnutil.c
f3e103
index 5ee5d76..7b7948a 100644
f3e103
--- a/lib/wnutil.c
f3e103
+++ b/lib/wnutil.c
f3e103
@@ -48,7 +48,7 @@ int wninit(void)
f3e103
     char *env;
f3e103
 
f3e103
     if (!done) {
f3e103
-	if (env = getenv("WNDBVERSION")) {
f3e103
+	if ((env = getenv("WNDBVERSION")) != NULL) {
f3e103
 	    wnrelease = strdup(env);	/* set release */
f3e103
 	    assert(wnrelease);
f3e103
 	}
f3e103
@@ -70,7 +70,7 @@ int re_wninit(void)
f3e103
 
f3e103
     closefps();
f3e103
 
f3e103
-    if (env = getenv("WNDBVERSION")) {
f3e103
+    if ((env = getenv("WNDBVERSION")) != NULL) {
f3e103
 	wnrelease = strdup(env);	/* set release */
f3e103
 	assert(wnrelease);
f3e103
     }
f3e103
@@ -149,25 +149,25 @@ static int do_init(void)
f3e103
 	sprintf(searchdir, DEFAULTPATH);
f3e103
 #else
f3e103
     if ((env = getenv("WNSEARCHDIR")) != NULL)
f3e103
-	strcpy(searchdir, env);
f3e103
+	snprintf(searchdir, sizeof(searchdir), "%s", env);
f3e103
     else if ((env = getenv("WNHOME")) != NULL)
f3e103
-	sprintf(searchdir, "%s%s", env, DICTDIR);
f3e103
+	snprintf(searchdir, sizeof(searchdir), "%s%s", env, DICTDIR);
f3e103
     else
f3e103
 	strcpy(searchdir, DEFAULTPATH);
f3e103
 #endif
f3e103
 
f3e103
     for (i = 1; i < NUMPARTS + 1; i++) {
f3e103
-	sprintf(tmpbuf, DATAFILE, searchdir, partnames[i]);
f3e103
+	snprintf(tmpbuf, sizeof(tmpbuf), DATAFILE, searchdir, partnames[i]);
f3e103
 	if((datafps[i] = fopen(tmpbuf, "r")) == NULL) {
f3e103
-	    sprintf(msgbuf,
f3e103
+	    snprintf(msgbuf, sizeof(msgbuf),
f3e103
 		    "WordNet library error: Can't open datafile(%s)\n",
f3e103
 		    tmpbuf);
f3e103
 	    display_message(msgbuf);
f3e103
 	    openerr = -1;
f3e103
 	}
f3e103
-	sprintf(tmpbuf, INDEXFILE, searchdir, partnames[i]);
f3e103
+	snprintf(tmpbuf, sizeof(tmpbuf), INDEXFILE, searchdir, partnames[i]);
f3e103
 	if((indexfps[i] = fopen(tmpbuf, "r")) == NULL) {
f3e103
-	    sprintf(msgbuf,
f3e103
+	    snprintf(msgbuf, sizeof(msgbuf),
f3e103
 		    "WordNet library error: Can't open indexfile(%s)\n",
f3e103
 		    tmpbuf);
f3e103
 	    display_message(msgbuf);
f3e103
@@ -178,35 +178,35 @@ static int do_init(void)
f3e103
     /* This file isn't used by the library and doesn't have to
f3e103
        be present.  No error is reported if the open fails. */
f3e103
 
f3e103
-    sprintf(tmpbuf, SENSEIDXFILE, searchdir);
f3e103
+    snprintf(tmpbuf, sizeof(tmpbuf), SENSEIDXFILE, searchdir);
f3e103
     sensefp = fopen(tmpbuf, "r");
f3e103
 
f3e103
     /* If this file isn't present, the runtime code will skip printint out
f3e103
        the number of times each sense was tagged. */
f3e103
 
f3e103
-    sprintf(tmpbuf, CNTLISTFILE, searchdir);
f3e103
+    snprintf(tmpbuf, sizeof(tmpbuf), CNTLISTFILE, searchdir);
f3e103
     cntlistfp = fopen(tmpbuf, "r");
f3e103
 
f3e103
     /* This file doesn't have to be present.  No error is reported if the
f3e103
        open fails. */
f3e103
 
f3e103
-    sprintf(tmpbuf, KEYIDXFILE, searchdir);
f3e103
+    snprintf(tmpbuf, sizeof(tmpbuf), KEYIDXFILE, searchdir);
f3e103
     keyindexfp = fopen(tmpbuf, "r");
f3e103
 
f3e103
-    sprintf(tmpbuf, REVKEYIDXFILE, searchdir);
f3e103
+    snprintf(tmpbuf, sizeof(tmpbuf), REVKEYIDXFILE, searchdir);
f3e103
     revkeyindexfp = fopen(tmpbuf, "r");
f3e103
 
f3e103
-    sprintf(tmpbuf, VRBSENTFILE, searchdir);
f3e103
+    snprintf(tmpbuf, sizeof(tmpbuf), VRBSENTFILE, searchdir);
f3e103
     if ((vsentfilefp = fopen(tmpbuf, "r")) == NULL) {
f3e103
-	sprintf(msgbuf,
f3e103
+	snprintf(msgbuf, sizeof(msgbuf),
f3e103
 "WordNet library warning: Can't open verb example sentence file(%s)\n",
f3e103
 		tmpbuf);
f3e103
 	display_message(msgbuf);
f3e103
     }
f3e103
 
f3e103
-    sprintf(tmpbuf, VRBIDXFILE, searchdir);
f3e103
+    snprintf(tmpbuf, sizeof(tmpbuf), VRBIDXFILE, searchdir);
f3e103
     if ((vidxfilefp = fopen(tmpbuf, "r")) == NULL) {
f3e103
-	sprintf(msgbuf,
f3e103
+	snprintf(msgbuf, sizeof(msgbuf),
f3e103
 "WordNet library warning: Can't open verb example sentence index file(%s)\n",
f3e103
 		tmpbuf);
f3e103
 	display_message(msgbuf);
f3e103
diff --git a/src/wn.c b/src/wn.c
f3e103
index ddb27aa..5c6a255 100644
f3e103
--- a/src/wn.c
f3e103
+++ b/src/wn.c
f3e103
@@ -129,7 +129,7 @@ static void printusage(), printlicense(),
f3e103
        printsearches(char *, int, unsigned long);
f3e103
 static int error_message(char *);
f3e103
 
f3e103
-main(int argc,char *argv[])
f3e103
+int main(int argc,char *argv[])
f3e103
 {
f3e103
     display_message = error_message;
f3e103
     
f3e103
@@ -225,14 +225,14 @@ static int do_search(char *searchword, int pos, int search, int whichsense,
f3e103
 	printf("\n%s of %s %s\n%s",
f3e103
 	       label, partnames[pos], searchword, outbuf);
f3e103
 
f3e103
-    if (morphword = morphstr(searchword, pos))
f3e103
+    if ((morphword = morphstr(searchword, pos)) != NULL)
f3e103
 	do {
f3e103
 	    outbuf = findtheinfo(morphword, pos, search, whichsense);
f3e103
 	    totsenses += wnresults.printcnt;
f3e103
 	    if (strlen(outbuf) > 0) 
f3e103
 		printf("\n%s of %s %s\n%s",
f3e103
 		       label, partnames[pos], morphword, outbuf);
f3e103
-	} while (morphword = morphstr(NULL, pos));
f3e103
+	} while ((morphword = morphstr(NULL, pos)) != NULL);
f3e103
 
f3e103
     return(totsenses);
f3e103
 }