Menu

Variable 'pthread_mutex_t' is unknown

2025-07-14
2 days ago
  • Inderjit Singh

    Inderjit Singh - 2025-07-14

    Hi.

    I don't seem to be able to get out of this problem.

    In my file queue.h I have declared:

    #include <pthread.h> 
    
    #define Q_HDR_INT_SZ    4
    #define Q_HDR_SZ        (sizeof(pthread_mutex_t) + Q_HDR_INT_SZ)
    

    When using the macro, I get:

    Variable 'pthread_mutex_t' is unknown
    

    Anyone can guide me to why this error is being generated and to fix it?

     
  • CHR

    CHR - 2025-07-14

    Is the output really generated by cppcheck? I can't reproduce this with the code above.

     
  • Inderjit Singh

    Inderjit Singh - 2025-07-15

    Sorry CHR. I realised was say too brief in the description. Here is a more detailed output:

    cppcheck version: 2.13.0

    $ cppcheck --addon=test/pipeline/coding_stds/misra.json --error-exitcode=255 -I/home/user/devel/repos/project/common/src/utils /home/user/devel/common/src/comp/comp_queue.c

    And the result:

    .
    .
    .
    Checking project/common/src/comp/comp_queue.c: SYSCFG_COMP_USE...
    
    project/common/src/comp/comp_queue.c:284:10: error: Variable 'pthread_mutex_t' is unknown [misra-config]
        if ((Q_HDR_SZ+(Q_ELEM_HDR_SZ+pkt_sz)*inbuf_cap) != inbuf_sz)
             ^
    project/common/src/comp/comp_queue.c:291:10: error: Variable 'pthread_mutex_t' is unknown [misra-config]
        if ((Q_HDR_SZ+(Q_ELEM_HDR_SZ+pkt_sz)*outbuf_cap) != outbuf_sz)
             ^
    .
    .
    .
    

    And queue.h is in the /home/user/devel/repos/project/common/src/utils directory

     
  • Inderjit Singh

    Inderjit Singh - 2025-08-08

    If anybody is interested. The workaround is to add -Dpthread_mutex_t=int to get around this issue. I cannot find a better way to get around this.

    It seems to me any sizeof(typedef_of_a_struct_t) always fails in macro expansions. i.e.

    #MY_MACRO sizeof(typedef_of_a_struct_t)
    will always result in error: Variable 'typedef_of_a_struct_t' is unknown [misra-config]

    The cppcheck with misra addon seems to make these errors more and more troublesome in which they needs to be bypassed. In my view, this is creating vulnerabilities by bypassing these errors as code structure might/probably change in future. I have same issues in other cases where my own declared structures are showing the same errors.

    Anyone shares my concern?

     
  • Daniel Marjamäki

    you can solve it in some different ways.

    how does it work if you provide the include path to pthread.h ?

     
  • Daniel Marjamäki

    A code example that reproduce the misra-config would be nice. It's not reported below however I did expect such warning.

    daniel@dator:~/cppchecksolutions/addon$ cat m1.c 
    
    //typedef int pthread_mutex_t;
    
    #define Q_HDR_INT_SZ    4
    #define Q_HDR_SZ        (sizeof(pthread_mutex_t) + Q_HDR_INT_SZ)
    
    uint32_t foo(void) {
        return Q_HDR_SZ;
    }
    daniel@dator:~/cppchecksolutions/addon$ ../cppcheck/cppcheck --addon=misra --enable=all m1.c
    Checking m1.c ...
    m1.c:7:10: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-8.4]
    uint32_t foo(void) {
             ^
    m1.c:8:12: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-10.4]
        return Q_HDR_SZ;
               ^
    m1.c:7:10: style: The function 'foo' is never used. [unusedFunction]
    uint32_t foo(void) {
             ^
    
     

    Last edit: Daniel Marjamäki 2 days ago

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.