-fanalyzer: Questions on C vs CPP + use of GCC attr's like malloc()/access()

Gavin Ray ray.gavin97@gmail.com
Sat Nov 26 15:48:44 GMT 2022


I was using if (fd != -1) and was still getting the warning which confused me
My suggestion was maybe to add the exact condition the fd analyzer is
looking for to the warning so that folks know how to fix it/trigger
its 'true' branch.

e.g. instead of:
"fd may not be valid"

Something like this, or thereabouts:
"fd may not be valid (expecting fd >= 0)"

On Sat, Nov 26, 2022 at 9:09 AM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
>
>
> On Fri, 25 Nov 2022, 18:55 Gavin Ray via Gcc, <gcc@gcc.gnu.org> wrote:
>>
>>
>> On a related note, the "fd" analyzers warn: "fd may not be valid" but don't
>> clarify what "valid" means.
>
>
> A valid file descriptor is one that was returned by the C library and refers to an open file. That's not something GCC defines.
>
>>
>> I found the DOT diagrams in the analyzer source and it turns out that the key is
>> to check "if (fd >= 0)".
>
>
> The point is that the OS functions that return a new file descriptor return a negative value on error, so all valid file descriptors are non-negative. But not all non-negative integers are valid file descriptors.
>
> You should check for errors when calling open, dup2, socket etc. so you know whether it succeeded.
>
>
>> Maybe this could be added to the "fd" analyzer
>> warnings/info, too?
>
>
> I don't think that's a good idea unless word carefully, it's not as simple as "test if it's non-negative". You should check for errors when using OS APIs, but that's always true, it's not difficult to the analyzer output.
>
>
>>
>>
>>
>>


More information about the Gcc mailing list