Skip to content

initializer-string for array of ... warnings #17687

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

Open
Disservin opened this issue Feb 3, 2025 · 8 comments · May be fixed by #18603
Open

initializer-string for array of ... warnings #17687

Disservin opened this issue Feb 3, 2025 · 8 comments · May be fixed by #18603

Comments

@Disservin
Copy link
Contributor

Disservin commented Feb 3, 2025

Description

Starting with gcc 15 the warning -Wunterminated-string-initialization is apparently enabled by default with -Wextra

This will cause initializations like these

const char x[2] = "ab";

to show a warning

initializer-string for array of 'char' is too long [-Wunterminated-string-initialization]

In the case of compiling the php-src this makes the entire compilation output completely unreadable and spams the terminal with debug diagnostics

just one example output line

/home/me/Github/php-src/ext/fileinfo/libmagic/../data_file.c:6780:1: warning: initializer-string for array of ‘unsigned char’ is too long [-Wunterminated-string-initialization]
 6780 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\002\0\030\0x\0\n\0\0\0\0\0\010\0\0\0\0\0\0\0\250\001\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0, resolutionunit=%d\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0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\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\001\0\010\0=\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\253\001\0\0\0\0\0\0\0\0\0\0001\001\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",

(ofc my terminal will add line breaks so it's much worse)

Overall the entire compilation log of make -j32 &> make.log has 26494 lines..
if needed I can share the compile output to help silencing this, not sure if there is an option to disable the warning

most warnings originate from php-src/ext/fileinfo/libmagic/../data_file.c

fyi this is allowed in c, but typically a user error if done so that's why it was enabled by default

C99 6.7.8/14: An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
@cmb69
Copy link
Member

cmb69 commented Feb 3, 2025

most warnings originate from php-src/ext/fileinfo/libmagic/../data_file.c

We bundle libmagic to be able to use the Zend memory manager, but will unlikely fix any issues. Consider to report that upstream (if not already reported/fixed there).

Wrt php-src: please compile again without ext/fileinfo (--disable-fileinfo), to see where that goes.

@nielsdos
Copy link
Member

nielsdos commented Feb 3, 2025

@cmb69 The data file is PHP's creation, not upstream's.

@Disservin
Copy link
Contributor Author

Disservin commented Feb 3, 2025

counts here

      2 /home/me/Github/php-src/ext/dom/lexbor/lexbor/css/syntax/state.c
     38 /home/me/Github/php-src/ext/dom/lexbor/lexbor/html/tokenizer/res.h
    266 /home/me/Github/php-src/ext/pdo/pdo_sqlstate.c
      1 /home/me/Github/php-src/ext/standard/crypt_sha256.c
      1 /home/me/Github/php-src/ext/standard/crypt_sha512.c

@cmb69
Copy link
Member

cmb69 commented Feb 4, 2025

Not sure what to do here. E.g. the PDO state is deliberately not zero-terminated, and should be properly treated this way in the code. I guess we could change that (either leave room for the terminating NUL, or use a char initializer), but I'm not sure it's worth it.

@Disservin
Copy link
Contributor Author

I wasn't so sure either.. this issue starts to pop up now in a few other repos as well, as people test gcc15, https://github.com/search?q=Wunterminated-string-initialization&type=issues.

You could fix these for now by either adding -Wno-unterminated-string-initialization though that might require a compiler version in your makefile to only apply for >= 15 or add something like this to the mentioned files

#if __GNUC__ >= 15
#pragma GCC diagnostic ignored "-Wunterminated-string-initialization"
#endif

@cmb69
Copy link
Member

cmb69 commented Feb 5, 2025

Maybe we should wait what happens with https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178.

@Disservin
Copy link
Contributor Author

seems like __attribute__ ((nonstring)) made it into gcc, im guessing this should be behind a version check.. regarding the code in /ext/ how is that updated ? For example for lexbor do I need to pr the changes to https://github.com/lexbor/lexbor and then wait for the next update of lexbor in php?

@nielsdos
Copy link
Member

I'll introduce a ZEND_ macro for the non string attribute.

seems like __attribute__ ((nonstring)) made it into gcc, im guessing this should be behind a version check.. regarding the code in /ext/ how is that updated ? For example for lexbor do I need to pr the changes to https://github.com/lexbor/lexbor and then wait for the next update of lexbor in php?

You need to PR the changes to that lexbor repository, and then it will get pulled in at the next sync indeed.

nielsdos added a commit to nielsdos/php-src that referenced this issue May 19, 2025
Starting with gcc 15 the warning `-Wunterminated-string-initialization`
is enabled by default. We make now use of the `nonstring` attribute to
silence the warning for the cases where this is intended.
This is targeted to 8.3 because of the people who compile with -Werror.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants