Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Getting undeclared error for the variable which is already declared.

shajabedi Posts: 8

I have this piece of code to compile with insure++

static int ABC
unsigned char *buffer,
int buffer_size,
int num,

    memset(buffer, '\0', buffer_size);
    void* xyz = #
    memcpy(buffer, xyz, "ABCD");

but after cmpilation, i get this error:
" error: 'xyz' undeclared (first use in this function)"

but it is already declared.


  • Rich
    Rich Posts: 34 ✭✭

    This code is not valid. memcpy takes three arguments:
    void *memcpy(void *dest, void *src, size_t count);

    You have a string literal for the third argument, instead of the count of number of bytes to copy.

    Also, what platform and compiler are you using?

  • shajabedi
    shajabedi Posts: 8

    Yeah, i was wrong it is
    memcpy(buffer, xyz, 5);
    I am in Linux and using gcc version 4.8.5

  • shajabedi
    shajabedi Posts: 8

    the question is xyz is declared but i am getting undeclared error for it in line memecpy.

  • Rich
    Rich Posts: 34 ✭✭
    edited December 2020

    I cannot reproduce this behavior with the information that you've given me so far.
    Please contact our technical support department, ([email protected]). Please include a small, compile-able test-case which reproduces this behavior.

    This is my test-case:

    #include <string.h>

    static void ABC (unsigned char *buffer, int buffer_size, int num)
    memset(buffer, '\0', buffer_size);
    void *xyz = &num;
    memcpy(buffer, xyz, 5);

    This compiles just fine with insure.

  • shajabedi
    shajabedi Posts: 8

    I compiled your test case and I got this error:
    error: 'xyz' undeclared (first use in this function)
    void *xyz = &num;

  • Hi @shajabedi ,

    I could confirm, the test case that @Rich provided compiled just fine with Insure++. I suspect someting wrong with either your setup or environment.
    As @Rich wrote, please, create reproducible test case that compiles with your compiler and reproduces the error, and contact our technical support.

  • cryptonian
    cryptonian Posts: 1

    The error you are encountering is due to the placement of the variable declaration
    void* xyz = &num;.

    It seems you have declared xyz inside the memset function block, making it out of scope for the memcpy function.

    To resolve this, move the
    void* xyz = &num;
    declaration to the beginning of the ABC function, before the memset and memcpy calls. This way, XYZ will be in scope for both functions, and the compilation error should be resolved. I have encountered a similar issue while working as a programmer for an industry-leading ai services provider