GCC-8-WSTRINGOP-Auslöser Was ist die gute Praxis?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: GCC-8-WSTRINGOP-Auslöser Was ist die gute Praxis?

by Anonymous » 19 Aug 2025, 13:18

gcc 8 wurde eine -wstringop -Aushandlung Warnung hinzugefügt. Von https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82944:


Die -WStringop -Warnung. Schneiden Sie den terminierenden NUL -Charcter aus der Quellzeichenfolge ab. Ein Beispiel für einen solchen Missbrauch in der Anfrage ist Folgendes: < /p>
< /blockquote>

Code: Select all

char buf[2];

void test (const char* str)
{
strncpy (buf, str, strlen (str));
}
< /code>

Ich bekomme die gleiche Warnung mit diesem Code. < /p>

strncpy(this->name, name, 32);

warning: 'char* strncpy(char*, const char*, size_t)' specified bound 32 equals destination size [-Wstringop-truncation`]
Bedenken Sie, dass dieser-> Name Zeichenname [32] und Name ist ein char* mit einer Länge, die möglicherweise größer als 32 ist. Ich bin verwirrt. Es ist nicht obligatorisch, dass dies-> Name nul-terminiert ist.

Top