Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Mats Kindahl <mats.kindahl@gmail.com>
To: cocci@inria.fr
Subject: [cocci] How to add error handling code before first use
Date: Mon, 11 Nov 2024 20:59:51 +0100	[thread overview]
Message-ID: <f59fb941-f9a9-455c-a369-f764e33323e8@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3313 bytes --]

Hi again,

I am trying to match code like the following and add error handling to it:

    #include <postgres.h>

    void foo(Oid id) {
       HeapTuple tuple;
       Form_pg_proc fd;

       tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(id));
       fd = (Form_pg_proc)GETSTRUCT(tuple);

       if (strcmp("magic", NameStr(fd->proname)) != 0)
         printf("Bad magic\n");
    }

The semantic patch I have for this is:

    @@
    identifier tuple;
    expression id, cache;
    statement S;
    @@
       tuple = SearchSysCache1(cache, ObjectIdGetDatum(id));
       ... when != tuple
    (
       if (<+... HeapTupleIsValid(tuple) ...+>) S
    |
    + if (!HeapTupleIsValid(tuple))
    +   elog(ERROR, "cache lookup failed for %u", id);
       (<+...tuple...+>);
    )

However, when executing spatch, I get the following error:

    mats@abzu:~/lang/cocci/postgres$ spatch --all-includes --sp-file
    heap_tuple.cocci code/heap_tuple.c
    init_defs_builtins: /usr/lib/coccinelle/standard.h
    before [[Tag29 ("{", 0)]; [Tag22 ((Tag1 ((Tag5 (("if", (11, 2, 0, 0,
    " "), Tag2 (0), 0), ("(", (11, 5, 0, 0, " "), Tag2 (0), 0), (Tag9
    ((Tag3 (((
    ((("HeapTupleIsValid", (11, 7, 0, 0, ""), Tag2 (0), 0)), 11, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)), 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 0),
    ("(", (11, 23, 0, 0, ""), Tag2 (0), 0), ([(((Tag1 ((("rule starting
    on line 1", "tuple"), (11, 24, 0, 0, ""), Tag2 (0), 0), 1, 0, 0),
    11, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)), 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 0)], 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), (")", (11,
    29, 0, 0,
    ""), Tag2 (0), 0)), 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), (6,
    (11, 6, 0, 0, ""), Tag2 (0), 0)), 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 0),
    (")", (11, 30, 0, 0, ""), Tag2 (0), 0)), 11, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 1, 0), (Tag8 ((Tag4 (((Tag3 (((((("elog", (12, 4, 0, 0, "
       "), Ta
    g2 (0), 0)), 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)), 12, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), ("(", (12, 8, 0, 0, ""), Tag2 (0),
    0), ([((
    ((("ERROR", (12, 9, 0, 0, ""), Tag2 (0), 0)), 12, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 1, 0)), 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0);
    (Tag26 (("
    ,", (12, 14, 0, 0, ""), Tag2 (0), 0)), 12, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 1, 0); (Tag1 ((("cache lookup failed for %u", 4), (12, 16,
    0, 0, " ")
    , Tag2 (0), 0)), 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); (Tag26
    ((",", (12, 44, 0, 0, ""), Tag2 (0), 0)), 12, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0
    , 1, 0); (Tag22 ((("rule starting on line 1", "id"), (12, 46, 0, 0,
    " "), Tag2 (0), 0), 1, 0, 0, 0, 0, 0), 12, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 1
    , 0)], 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), (")", (12, 48, 0,
    0, ""), Tag2 (0), 0)), 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)),
    (";", (1
    2, 49, 0, 0, ""), Tag2 (0), 0)), 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 0)), 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), (0, 0, 0,
    Tag1 (0, 0)))
    , 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0))]]
    context tree should not have bad code before

First, what is causing the error?

Second, is the semantic patch supposed to work or is another method 
preferred?

Best wishes,
Mats Kindahl

[-- Attachment #2: Type: text/html, Size: 4746 bytes --]

             reply	other threads:[~2024-11-11 20:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-11 19:59 Mats Kindahl [this message]
2024-11-11 22:36 ` [cocci] How to add error handling code before first use Julia Lawall
2024-11-13 15:13   ` Julia Lawall
2024-11-13 17:38     ` Mats Kindahl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f59fb941-f9a9-455c-a369-f764e33323e8@gmail.com \
    --to=mats.kindahl@gmail.com \
    --cc=cocci@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).