Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various static analyzer fixes #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

frozencemetery
Copy link

For the first, I've opted for a more invasive change to the function since the control was harder to reason about, but I could provide a more minimal fix to the problems reported if that would be preferred.

Static analyzer notes about the issues are in the commit messages, which I can also remove if that would be preferred.

Flatten control flow to use a single exit path.  Fixes leaks, including
of dmi_fname and entry_fname.  Reported by Coverity:

    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_linux.c:104: alloc_arg: "asprintf" allocates memory that is stored into "dmi_fname". [Note: The source code implementation of the function has been overridden by a builtin model.]
    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_linux.c:131: noescape: Resource "dmi_fname" is not freed or pointed-to in "read_file".
    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_linux.c:160: leaked_storage: Variable "dmi_fname" going out of scope leaks the storage it points to.
    #  158|       free(entry_buffer);
    #  159|       fnprintf(" out: %d\n", retval);
    #  160|->     return retval;
    #  161|   }
    #  162|

    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_linux.c:100: alloc_arg: "asprintf" allocates memory that is stored into "entry_fname". [Note: The source code implementation of the function has been overridden by a builtin model.]
    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_linux.c:111: noescape: Resource "entry_fname" is not freed or pointed-to in "read_file".
    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_linux.c:160: leaked_storage: Variable "entry_fname" going out of scope leaks the storage it points to.
    #  158|       free(entry_buffer);
    #  159|       fnprintf(" out: %d\n", retval);
    #  160|->     return retval;
    #  161|   }
    #  162|

Signed-off-by: Robbie Harwood <[email protected]>
Reported by Coverity:

    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_obj.c:82: address: Taking address of "singleton".
    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_obj.c:82: assign: Assigning: "toReturn" = "&singleton".
    libsmbios-2.4.3/src/libsmbios_c/smbios/smbios_obj.c:103: incorrect_free: "init_smbios_struct" frees incorrect pointer "toReturn".
    #  101|       }
    #  102|
    #  103|->     ret = init_smbios_struct(toReturn);
    #  104|       if (ret)
    #  105|           goto out_init_fail;

Signed-off-by: Robbie Harwood <[email protected]>
@frozencemetery frozencemetery changed the title Two static analyzer fixes Three static analyzer fixes Sep 8, 2021
@frozencemetery frozencemetery changed the title Three static analyzer fixes Various static analyzer fixes Sep 8, 2021
In the failure case, toReturn has been freed already, so dereferencing
it to write to a struct member (initialized) is not valid.

Signed-off-by: Robbie Harwood <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant