llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[libc] Request for Annex K via `__STDC_WANT_LIB_EXT1__` doesn't work as expected

Open vhscampos opened this issue 7 months ago • 9 comments

Source code:

#include <stdio.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>

int foo() {
  return strnlen_s("abc", 2);
}

Compile with: clang -c a.c

We get the error:

a.c:6:10: error: call to undeclared function 'strnlen_s'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    6 |   return strnlen_s("abc", 2);
      |          ^

If we move the line #define __STDC_WANT_LIB_EXT1__ 1 before #include <stdio.h>, it works as expected.

Analysis

I believe this issue is caused by the header inclusion guard in llvm-libc-macros/annex-k-macros.h.

The inclusion of stdio.h implies a recursive inclusion of annex-k-macros.h. But at that point, the __STDC_WANT_LIB_EXT1__ is not yet defined, therefore the macro who controls the availability of Annex K functionality, LIBC_HAS_ANNEX_K, is not defined.

By the time __STDC_WANT_LIB_EXT1__ is defined and string.h is included, the recursive inclusion of annex-k-macros.h (by the second time now) is stopped at the header inclusion guard, hence LIBC_HAS_ANNEX_K is again not defined. As a result, the declaration of strnlen_s is never brought in.

Found by @simpal01

vhscampos avatar May 01 '26 15:05 vhscampos

@llvm/issue-subscribers-libc

Author: Victor Campos (vhscampos)

Source code: ```c #include <stdio.h> #define __STDC_WANT_LIB_EXT1__ 1 #include <string.h>

int foo() { return strnlen_s("abc", 2); }

Compile with:
`clang a.c`

We get the error:

a.c:6:10: error: call to undeclared function 'strnlen_s'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 6 | return strnlen_s("abc", 2); | ^


If we move the line `#define __STDC_WANT_LIB_EXT1__ 1` before `#include &lt;stdio.h&gt;`, it works as expected.

**Analysis**

I believe this issue is caused by the header inclusion guard in `llvm-libc-macros/annex-k-macros.h`.

The inclusion of `stdio.h` implies a recursive inclusion of `annex-k-macros.h`. But at that point, the `__STDC_WANT_LIB_EXT1__` is not yet defined, therefore the macro who controls the availability of Annex K functionality, `LIBC_HAS_ANNEX_K`, is not defined.

By the time `__STDC_WANT_LIB_EXT1__` is defined and `string.h` is included, the recursive inclusion of `annex-k-macros.h` (by the second time now) is stopped at the header inclusion guard, hence `LIBC_HAS_ANNEX_K` is again not defined. As a result, the declaration of `strnlen_s` is never brought in.
</details>

One solution is to remove the header inclusion guard from annex-k-macros.h and surround the #define LIBC_HAS_ANNEX_K by an #ifndef condition.

vhscampos avatar May 01 '26 15:05 vhscampos

@vhscampos Hi, is anyone currently working on this issue? If not, I’d like to take it on.

ChenMiaoi avatar Oct 03 '26 02:10 ChenMiaoi

Hi @ChenMiaoi. No, it is untaken. Feel free to! Please add me as a reviewer once you have a PR.

vhscampos avatar Oct 06 '26 08:10 vhscampos

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Victor Campos (vhscampos)

Source code: ```c #include <stdio.h> #define __STDC_WANT_LIB_EXT1__ 1 #include <string.h>

int foo() { return strnlen_s("abc", 2); }

Compile with:
`clang -c a.c`

We get the error:

a.c:6:10: error: call to undeclared function 'strnlen_s'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 6 | return strnlen_s("abc", 2); | ^


If we move the line `#define __STDC_WANT_LIB_EXT1__ 1` before `#include &lt;stdio.h&gt;`, it works as expected.

**Analysis**

I believe this issue is caused by the header inclusion guard in `llvm-libc-macros/annex-k-macros.h`.

The inclusion of `stdio.h` implies a recursive inclusion of `annex-k-macros.h`. But at that point, the `__STDC_WANT_LIB_EXT1__` is not yet defined, therefore the macro who controls the availability of Annex K functionality, `LIBC_HAS_ANNEX_K`, is not defined.

By the time `__STDC_WANT_LIB_EXT1__` is defined and `string.h` is included, the recursive inclusion of `annex-k-macros.h` (by the second time now) is stopped at the header inclusion guard, hence `LIBC_HAS_ANNEX_K` is again not defined. As a result, the declaration of `strnlen_s` is never brought in.

Found by @<!-- -->simpal01 
</details>

Hi @ChenMiaoi if you're not currently working on this issue ,could I work on this issue?

harshasr60 avatar Nov 03 '26 08:11 harshasr60

13. Hi @ChenMiaoi if you're not currently working on this issue ,could I work on this issue?

Of course! My focus is currently on Clang frontend. When you open your PR, please remind me to close mine.

ChenMiaoi avatar Nov 03 '26 08:11 ChenMiaoi

@vhscampos sir can I work on this issue