Sujet : Fixing a sample from K&R book using cake static analyser
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 21. Jun 2024, 13:45:21
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v53sl1$35qt7$1@dont-email.me>
User-Agent : Mozilla Thunderbird
In this video (
https://youtu.be/ZZCKPKzNUCQ) I show step by step how removing warnings will fix a memory leak.
Page 145, The C programming Language 2 Edition
/* install: put (name, defn) in hashtab */
struct nlist *install(char *name, char *defn)
{
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 */
if ((np->defn = strdup(defn)) == NULL)
return NULL;
return np;
}
The concepts used are
- ownership transfer
- nullable pointers
Concepts are described here
http://thradams.com/cake/ownership.htmlTo see the sample and play with it:
http://thradams.com/cake/playground.htmlSelect "find the bug" and "bug #7 K & R"