| Deutsch English Français Italiano |
|
<v5aeue$j1nj$4@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D'Oliveiro <ldo@nz.invalid>
Newsgroups: comp.lang.c
Subject: Re: Fixing a sample from K&R book using cake static analyser
Date: Mon, 24 Jun 2024 00:34:22 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <v5aeue$j1nj$4@dont-email.me>
References: <v53sl1$35qt7$1@dont-email.me> <v558hv$3dskb$1@dont-email.me>
<20240623034624.135@kylheku.com> <87wmmfq4if.fsf@bsb.me.uk>
<20240624012527.8bbe16b96f5bfca10feadb5c@gmail.moc>
<87zfrbnsvv.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 24 Jun 2024 02:34:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="3048fea01d51f9337586ac8e02824e6c";
logging-data="624371"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+soyctxaOC8xUgdh6H8jrA"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:gU/BrpIyl0Vb1xkw9AgKLSThpDo=
Bytes: 2606
On Mon, 24 Jun 2024 00:25:56 +0100, Ben Bacarisse wrote:
> struct nlist *install(const char *name, const char *defn)
> {
> struct nlist *node = lookup(name);
> if (node) {
> char *new_defn = strdup(defn);
> if (new_defn) {
> free(node->defn);
> node->defn = new_defn;
> return node;
> }
> else return NULL;
> }
> else {
> struct nlist *new_node = malloc(sizeof *new_node);
> char *new_name = strdup(name), *new_defn = strdup(defn);
> if (new_node && new_name && new_defn) {
> unsigned hashval = hash(name);
> new_node->name = new_name;
> new_node->defn = new_defn;
> new_node->next = hashtab[hashval];
> return hashtab[hashval] = new_node;
> }
> else {
> free(new_defn);
> free(new_name);
> free(new_node);
> return NULL;
> }
> }
> }
>
> We have four cases:
>
> node with the name found
> new definition allocated new definition not allocated
> node with the name not found
> new node, name and definition all allocated not all of new node,
> name and definition allocated.
>
> We can very simply reason about all of these situations.
Too many different paths in the control flow, though. I think it’s a good
idea to minimize that.