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

Avoid GNU getcwd extension behavior #188

Merged
merged 2 commits into from
Jan 7, 2025

Conversation

bbannier
Copy link
Contributor

GNU getcwd can allocate a buffer if passed NULL for it. This is an extension which is e.g., not recognized by clang-tidy-19's StdCLibraryFunctions check1 which emits a diagnostic on a violated precondition buf != NULL,

The 1st argument to 'getcwd' is NULL but should not be NULL [clang-analyzer-unix.StdCLibraryFunctions,-warnings-as-errors]
[build]  3987 |     std::unique_ptr<char, decltype(&std::free)> buffer{::getcwd(NULL, 0), std::free};

This patch modifies this use of getcwd with this extension behavior to instead use get_current_dir_name which is also a GNU extension, but does not trigger this diagnostic.

Footnotes

  1. https://clang.llvm.org/docs/analyzer/checkers.html#unix-stdclibraryfunctions-c

GNU `getcwd` can allocate a buffer if passed `NULL` for it. This is an
extension which is e.g., not recognized by clang-tidy-19's
`StdCLibraryFunctions` check[^1] which emits a diagnostic on a violated
precondition `buf != NULL`,

```
The 1st argument to 'getcwd' is NULL but should not be NULL [clang-analyzer-unix.StdCLibraryFunctions,-warnings-as-errors]
[build]  3987 |     std::unique_ptr<char, decltype(&std::free)> buffer{::getcwd(NULL, 0), std::free};
```

This patch modifies this use of `getcwd` with this extension behavior to
instead use `get_current_dir_name` which is also a GNU extension, but
does not trigger this diagnostic.

[^1]: https://clang.llvm.org/docs/analyzer/checkers.html#unix-stdclibraryfunctions-c
centos7 has reached EOL on 2024-06-30, centos8 on 2021-10-31. In
practical terms this means that their package repositories are offline
for the foreseeable future and the CI tasks making use of them might
never run again.

This patch replaces the existing CI jobs on centos7/centos8 with jobs on
rockylinux8/9. Since rockylinux is an open source variant similar to
RHEL this should provide testing with similar spirit. In contrast to
centos, rockylinux does provide very granular tagging of releases which
if used would help avoid breaking on e.g., subtle package changes in
their package repositories. I however did not use very precise tags in
the patch and instead went with broad "trains" for the 8 and 9 series;
I hope this minimizes maintenance overhead (e.g., bumping to new
releases) while still giving a good testing signal.
@bbannier
Copy link
Contributor Author

I added a commit switching from centos CI over to rockylinux (latest two releases). Let me know whether you would like me to pull that into a separate PR.

@bbannier bbannier marked this pull request as ready for review December 18, 2024 15:00
@bbannier
Copy link
Contributor Author

bbannier commented Jan 7, 2025

@gulrak, anything I could do to pique your interest in this patch? I am having trouble suppressing it in my clang-tidy setup, but would prefer keeping the check globally enabled.

An alternative approach would be to suppress the check for just this line (I see that there are already some suppression in this project), e.g., by prefixing it with

   // NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions)

@gulrak
Copy link
Owner

gulrak commented Jan 7, 2025

Sorry, for me not reacting quicker, but you are right, that is a better solution. I was, for a moment, thinking if one might switch to something along the lines (but slightly to be adapted) of the no extension needing suggested code snippet from the documentation, like

char *
gnu_getcwd ()
{
  size_t size = 100;

  while (1)
    {
      char *buffer = (char *) xmalloc (size);
      if (getcwd (buffer, size) == buffer)
        return buffer;
      free (buffer);
      if (errno != ERANGE)
        return 0;
      size *= 2;
    }
}

But on the base that it currently uses the extensions, I think your solution is a quick and easy one, and I'm all for not having exceptions in tidy.

I'm at work currently, but will merge it later today.

Thanks for the PR and the nagging. ;-)

@gulrak gulrak merged commit 076592c into gulrak:master Jan 7, 2025
15 of 17 checks passed
@gulrak gulrak added available on master Fix is done on master branch, issue closed on next release POSIX POSIX type backend is affected labels Jan 7, 2025
@bbannier bbannier deleted the topic/bbannier/getcwd-with-NULL-buf branch January 7, 2025 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
available on master Fix is done on master branch, issue closed on next release POSIX POSIX type backend is affected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants