madler / zlib

A massively spiffy yet delicately unobtrusive compression library.
http://zlib.net/
Other
5.46k stars 2.41k forks source link

Clean write-strings warning #959

Closed Lwisce closed 3 months ago

Lwisce commented 3 months ago

If we add -Wwrite-strings into CFLAGS in configure file. And then run

./configure -w --const make test

We will get compiler warning :

infback.c: In function ‘inflateBack’: infback.c:307:29: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual] 307 | strm->msg = (char )"invalid block type"; | ^ infback.c:318:29: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual] 318 | strm->msg = (char )"invalid stored block lengths"; | ^ infback.c:356:29: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual] 356 | strm->msg = (char *)"too many length or distance symbols"; | ^

The reason for this warning is code attempts to cast a const char pointer pointing to a constant string literal to a char pointer.

Also, in the code, msg is actually declared asz_const char*, so it is logical to add z_const in the type conversion.

madler commented 3 months ago

Applied. Thanks.