New compiler releases often bring with them new warnings; those warnings are usually welcome, since they help developers find problems before they turn into nasty bugs. Adapting to new warnings can also create disruption in the development process, though, especially when an important developer upgrades to a new compiler at an unfortunate time. This is just the scenario that played out with the 6.15-rc3 kernel release and the implementation of -Wunterminated-string-initialization in GCC 15.
新的编译器版本通常会引入新的警告;这些警告通常是受欢迎的,因为它们有助于开发者在问题变成严重 bug 之前发现它们。然而,适应新的警告也可能会对开发流程造成干扰,尤其是在某个关键开发者在一个不太合适的时间点升级了编译器的情况下。6.15-rc3 内核的发布和 GCC 15 实现的 -Wunterminated-string-initialization
警告正是这种情况的一个例子。
Consider a C declaration like:
想象如下的 C 语言声明:
char foo[8] = "bar";
The array will be initialized with the given string, including the normal trailing NUL byte indicating the end of the string. Now consider this variant:
这个数组会使用给定的字符串进行初始化,并包含表示字符串结尾的标准结尾 NUL 字节。现在再看下面这个变体:
char foo[8] = "NUL-free";
This is a legal declaration, even though the declared array now lacks the room for the NUL byte. That byte will simply be omitted, creating an unterminated string. That is often not what the developer who wrote that code wants, and it can lead to unpleasant bugs that are not discovered until some later time. The -Wunterminated-string-initialization option emits a warning for this kind of initialization, with the result that, hopefully, the problem — if there is a problem — is fixed quickly.
这同样是合法的声明,尽管声明的数组空间不足以容纳 NUL 字节。该字节会被简单地省略,从而创建一个未终止的字符串。这通常并非开发者的本意,并可能导致一些不易发现但令人头疼的 bug。GCC 的 -Wunterminated-string-initialization
选项会对这类初始化发出警告,从而有望在问题造成影响前尽早解决。</