Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:22:58+00:00 2026-05-26T02:22:58+00:00

Some functions have a pointer argument that points to where a result from calling

  • 0

Some functions have a pointer argument that points to where a result from
calling the function should be stored, but the function also require that
when calling the function this pointer points to some value used as input value
(e.g. an in/out parameter).

I want to detect cases where such functions are called pointing to uninitialized variables.
Coccinelle should be able to do this, however I struggle a little to achieve this.

Example target code:

#include <string.h>
#include <stdio.h>

static void cartoon_random_generator(int *n)
{
    switch (*n) {
    case 4:
        *n = 4; /* http://xkcd.com/221/ */
        break;
    case 9:
        *n = 9; /* http://dilbert.com/strips/comic/2001-10-25/ */
        break;
    default:
        fprintf(stderr, "*n was not initialized before calling this function\n");
        break;
    }
}
/* alternative links https://i.stack.imgur.com/VvTef.png and https://i.stack.imgur.com/u0iJ7.gif */

static void test(const char *cartoon)
{
    // not ok, missing
    {
        int n1;

        cartoon_random_generator(&n1);
        printf("Random number = %d\n", n1);
    }

    // ok, declaration
    {
        int n2 = 4;

        cartoon_random_generator(&n2);
        printf("Random number = %d\n", n2);
    }

    // ok, statement
    {
        int n3;

        n3 = 9;
        cartoon_random_generator(&n3);
        printf("Random number = %d\n", n3);
    }

    // both ok and not ok
    {
        int n4, n9;

        n9 = 9;
        //strcmp(cartoon, "XKCD") == 0 ? cartoon_random_generator(&n4) : cartoon_random_generator(&n9);
        if (strcmp(cartoon, "XKCD") == 0)
            cartoon_random_generator(&n4);
        else
            cartoon_random_generator(&n9);
        printf("Random numbers = %d, %d\n", n4, n9);
    }
}

I have written the following coccinelle script

/* It is an error to call cartoon_random_generator with an uninitialized
   variable. Detect this. */


/*
 * This rule matches an OK case where the in variable is initialized when
 * declared. No action is performed for this rule other than giving p1 a value.
 */
@rule1@
position p1;
expression init_expression;
identifier n;
@@

int n = init_expression;
...
cartoon_random_generator@p1(&n)


/*
 * This rule matches an OK case where the in variable is initialized in a
 * separate statement. No action is performed for this rule other than
 * giving p2 a value.
 */
@rule2@
position p2;
expression init_expression;
identifier n;
@@

int n;
...
n = init_expression;
...
cartoon_random_generator@p2(&n)


/* If neither rule1 or rule2 have matched so far,
 * we have a variable that is uninitialized. */

@rule3@
position p3 != rule1.p1, rule2.p2;
identifier n;
@@

int n;
...
* cartoon_random_generator@p3(&n)

but rule2 is not taken into account and I do not understand why. Running it gives:

$ /opt/coccinelle/bin/spatch -sp_file cartoon_random.cocci cartoon_random.c
init_defs_builtins: /opt/coccinelle/share/coccinelle/standard.h
warning: rule3: inherited metavariable p2 not used in the -, +, or context code
HANDLING: cartoon_random.c
diff =
--- cartoon_random.c
+++ /tmp/cocci-output-7916-8df75b-cartoon_random.c
@@ -23,7 +23,6 @@ static void test(const char *cartoon)
        {
                int n1;

-               cartoon_random_generator(&n1);
                printf("Random number = %d\n", n1);
        }

@@ -40,7 +39,6 @@ static void test(const char *cartoon)
                int n3;

                n3 = 9;
-               cartoon_random_generator(&n3);
                printf("Random number = %d\n", n3);
        }

@@ -51,9 +49,7 @@ static void test(const char *cartoon)
                n9 = 9;
                //strcmp(cartoon, "XKCD") == 0 ? cartoon_random_generator(&n4) : cartoon_random_generator(&n9);
                if (strcmp(cartoon, "XKCD") == 0)
-                       cartoon_random_generator(&n4);
                else
-                       cartoon_random_generator(&n9);
                printf("Random numbers = %d, %d\n", n4, n9);
        }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:22:59+00:00Added an answer on May 26, 2026 at 2:22 am

    I am a total beginner in the usage of Coccinelle and had been meaning to learn about it. The question you have posed is a fairly good requirement of detecting uninitialized variable and this made me research a bit. After doing a little bit of research (& getting a clue from warning: rule3: inherited metavariable p2 not used in the -, +, or context code ) one of ways (there could be other/better ways) to make your coccinelle script work is to combine rules 1 and 2 & use only single inheritance for the metavariable in the final rule. Something on these lines:

    @rule1@
    position p1;
    expression init_expression;
    identifier n;
    @@
    
    (
    int n = init_expression;
    |
    int n;
    ...
    n = init_expression;
    )
    ...
    cartoon_random_generator@p1(&n)
    
    @rule2@
    position p2 != rule1.p1;
    identifier n;
    @@
    
    int n;
    ...
    * cartoon_random_generator@p2(&n)  
    

    The output seen in this case is:

    $spatch -sp_file cartoon_random.cocci cartoon_random.c
    init_defs_builtins: /usr/share/coccinelle/standard.h
    HANDLING: cartoon_random.c
    diff =
    --- cartoon_random.c
    +++ /tmp/cocci-output-7916-8df75b-cartoon_random.c
    @@ -23,7 +23,6 @@ static void test(const char *cartoon)
            {
                    int n1;
    
    -               cartoon_random_generator(&n1);
                    printf("Random number = %d\n", n1);
            }
    
    @@ -51,9 +50,6 @@ static void test(const char *cartoon)
                    n9 = 9;
                    //strcmp(cartoon, "XKCD") == 0 ? cartoon_random_generator(&n4) : cartoon_random_generator(&n9);
                    if (strcmp(cartoon, "XKCD") == 0)
    -                       cartoon_random_generator(&n4);
                    else
                            cartoon_random_generator(&n9);
                    printf("Random numbers = %d, %d\n", n4, n9);
    

    This was ran on FC15 with coccinelle packages installed from Fedora repos.
    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some global variables in a Python script. Some functions in that script
I would like to have a function that modifies some variable list of parameters
I have some code that uses the Oracle function add_months to increment a Date
I have argument with my friend. He says that I can return a pointer
In several places I have to retrieve some value from a dict, but need
The function below takes the argv[0] argument that contains the calling path of the
When I have some function of type like f :: (Ord a) => a
I have some function to find a value: struct FindPredicate { FindPredicate(const SomeType& t)
Is there a way to have a function return a editable reference to some
I'm working with LINQ to objects and have a function where in some cases

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.