Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Fixing a sample from K&R book using cake static analyser Date: Fri, 21 Jun 2024 20:19:37 -0700 Organization: A noiseless patient Spider Lines: 60 Message-ID: <86le2xhdfa.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sat, 22 Jun 2024 05:19:37 +0200 (CEST) Injection-Info: dont-email.me; posting-host="ec2cba2faddfc9625d8883c2f98b569e"; logging-data="3765847"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199L9nfiDiGkt5d1fzM2e3+Awxy1iOzROw=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:0TffSQILbYPAjudmnlX9mPIEPC0= sha1:NKIChIGzcKa2Uc9ELy6ai+qb2L8= Bytes: 2852 Lawrence D'Oliveiro writes: > On Fri, 21 Jun 2024 09:45:21 -0300, Thiago Adams wrote: > >> 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; >> } > > struct nlist *install(char *name, char *defn) > { > struct nlist *np = NULL; > struct nlist *result = NULL; > unsigned hashval; > do /*once*/ > { > result = lookup(name); > if (result != NULL) > break; > np = (struct nlist *)calloc(1, sizeof struct nlist); > if (np == NULL) > break; > np->defn = strdup(defn); > if (np->defn == NULL) > break; > hashval = hash(name); > np->next = hashtab[hashval]; > hashtab[hashval] = np; > result = np; > np = NULL; /* so I don?t dispose of it yet */ > } > while (false); > if (np != NULL) > { > free(np->defn); > } /*if*/ > free(np); > return > result; > } /*install*/ Both of these are truly awful.