-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
1,148 additions
and
1,132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,55 @@ | ||
|
||
/* | ||
This code is from | ||
The C Programming Language 2 edition, page 145 | ||
There are two bugs int this original sample | ||
- one memory leak | ||
- one invalid state | ||
*/ | ||
|
||
#pragma safety enable | ||
|
||
struct X { | ||
int i; | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
struct nlist { /* table entry: */ | ||
struct nlist *next; /* next entry in chain */ | ||
char *name; /* defined name */ | ||
char *defn; /* replacement text */ | ||
}; | ||
|
||
void f(struct X* _Opt p) | ||
struct nlist *lookup(char *s); | ||
|
||
/* hash: form hash value for string s */ | ||
unsigned hash(char *s); | ||
|
||
|
||
#define HASHSIZE 101 | ||
|
||
static struct nlist *hashtab[HASHSIZE]; /* pointer table */ | ||
|
||
/*1* lookup: look for s in hashtab */ | ||
struct nlist *lookup(char *); | ||
|
||
|
||
/* install: put (name, defn) in hashtab */ | ||
struct nlist *install(char *name, char *defn) | ||
{ | ||
int i = p ? p->i : 0; //no warning | ||
|
||
int i3 = p->i; | ||
#pragma cake diagnostic check "-Wanalyzer-null-dereference" | ||
struct nlist *np; | ||
unsigned hashval; | ||
|
||
if ((np = lookup(name)) == NULL) { /* not found */ | ||
np = (struct nlist *) malloc(sizeof(*np)); | ||
if (np == NULL || (np->name = strdup(name)) == NULL) | ||
return NULL; | ||
hashval = hash(name); | ||
np->next = hashtab[hashval]; | ||
hashtab[hashval] = np; | ||
} else /* already there */ | ||
free((void *) np->defn); /* free previous defn */ | ||
|
||
int i2 = p ? 0 : p->i; //warning | ||
#pragma cake diagnostic check "-Wanalyzer-null-dereference" | ||
if ((np->defn = strdup(defn)) == NULL) | ||
return NULL; | ||
return np; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.