On Fri, 25 Nov 2022, 18:55 Gavin Ray via Gcc, 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. > > > >