Skip to content

Commit 62d6112

Browse files
committed
Clean up the usage of z_const and respect const usage within zlib.
This patch allows zlib to compile cleanly with the -Wcast-qual gcc warning enabled, but only if ZLIB_CONST is defined, which adds const to next_in and msg in z_stream and in the in_func prototype. A --const option is added to ./configure which adds -DZLIB_CONST to the compile flags, and adds -Wcast-qual to the compile flags when ZLIBGCCWARN is set in the environment.
1 parent fb4e059 commit 62d6112

File tree

16 files changed

+76
-70
lines changed

16 files changed

+76
-70
lines changed

compress.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
2929
z_stream stream;
3030
int err;
3131

32-
stream.next_in = (Bytef*)source;
32+
stream.next_in = (z_const Bytef *)source;
3333
stream.avail_in = (uInt)sourceLen;
3434
#ifdef MAXSEG_64K
3535
/* Check for source > 64K on 16-bit machine: */

configure

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ shared=1
7070
solo=0
7171
cover=0
7272
zprefix=0
73+
zconst=0
7374
build64=0
7475
gcc=0
7576
old_cc="$CC"
@@ -96,7 +97,7 @@ do
9697
case "$1" in
9798
-h* | --help)
9899
echo 'usage:' | tee -a configure.log
99-
echo ' configure [--zprefix] [--prefix=PREFIX] [--eprefix=EXPREFIX]' | tee -a configure.log
100+
echo ' configure [--const] [--zprefix] [--prefix=PREFIX] [--eprefix=EXPREFIX]' | tee -a configure.log
100101
echo ' [--static] [--64] [--libdir=LIBDIR] [--sharedlibdir=LIBDIR]' | tee -a configure.log
101102
echo ' [--includedir=INCLUDEDIR] [--archs="-arch i386 -arch x86_64"]' | tee -a configure.log
102103
exit 0 ;;
@@ -119,6 +120,7 @@ case "$1" in
119120
-a*=* | --archs=*) ARCHS=`echo $1 | sed 's/.*=//'`; shift ;;
120121
--sysconfdir=*) echo "ignored option: --sysconfdir" | tee -a configure.log; shift ;;
121122
--localstatedir=*) echo "ignored option: --localstatedir" | tee -a configure.log; shift ;;
123+
-c* | --const) zconst=1; shift ;;
122124
*)
123125
echo "unknown option: $1" | tee -a configure.log
124126
echo "$0 --help for help" | tee -a configure.log
@@ -171,7 +173,11 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
171173
SFLAGS="${SFLAGS} -m64"
172174
fi
173175
if test "${ZLIBGCCWARN}" = "YES"; then
174-
CFLAGS="${CFLAGS} -Wall -Wextra -pedantic"
176+
if test "$zconst" -eq 1; then
177+
CFLAGS="${CFLAGS} -Wall -Wextra -Wcast-qual -pedantic -DZLIB_CONST"
178+
else
179+
CFLAGS="${CFLAGS} -Wall -Wextra -pedantic"
180+
fi
175181
fi
176182
if test -z "$uname"; then
177183
uname=`(uname -s || echo unknown) 2>/dev/null`

contrib/infback9/infback9.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ out_func out;
222222
void FAR *out_desc;
223223
{
224224
struct inflate_state FAR *state;
225-
unsigned char FAR *next; /* next input */
225+
z_const unsigned char FAR *next; /* next input */
226226
unsigned char FAR *put; /* next output */
227227
unsigned have; /* available input */
228228
unsigned long left; /* available output */

deflate.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
305305
if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
306306
s->pending_buf == Z_NULL) {
307307
s->status = FINISH_STATE;
308-
strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
308+
strm->msg = ERR_MSG(Z_MEM_ERROR);
309309
deflateEnd (strm);
310310
return Z_MEM_ERROR;
311311
}
@@ -329,7 +329,7 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
329329
uInt str, n;
330330
int wrap;
331331
unsigned avail;
332-
unsigned char *next;
332+
z_const unsigned char *next;
333333

334334
if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
335335
return Z_STREAM_ERROR;
@@ -359,7 +359,7 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
359359
avail = strm->avail_in;
360360
next = strm->next_in;
361361
strm->avail_in = dictLength;
362-
strm->next_in = (Bytef *)dictionary;
362+
strm->next_in = (z_const Bytef *)dictionary;
363363
fill_window(s);
364364
while (s->lookahead >= MIN_MATCH) {
365365
str = s->strstart;

gzlib.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ const char * ZEXPORT gzerror(file, errnum)
541541
/* return error information */
542542
if (errnum != NULL)
543543
*errnum = state->err;
544-
return state->msg == NULL ? "" : state->msg;
544+
return state->err == Z_MEM_ERROR ? "out of memory" :
545+
(state->msg == NULL ? "" : state->msg);
545546
}
546547

547548
/* -- see zlib.h -- */
@@ -592,16 +593,13 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
592593
if (msg == NULL)
593594
return;
594595

595-
/* for an out of memory error, save as static string */
596-
if (err == Z_MEM_ERROR) {
597-
state->msg = (char *)msg;
596+
/* for an out of memory error, return literal string when requested */
597+
if (err == Z_MEM_ERROR)
598598
return;
599-
}
600599

601600
/* construct error message with path */
602601
if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
603602
state->err = Z_MEM_ERROR;
604-
state->msg = (char *)"out of memory";
605603
return;
606604
}
607605
#if !defined(NO_snprintf) && !defined(NO_vsnprintf)

gzread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ local int gz_avail(state)
5858
return -1;
5959
if (state->eof == 0) {
6060
if (strm->avail_in) { /* copy what's there to the start */
61-
unsigned char *p = state->in, *q = strm->next_in;
61+
unsigned char *p = state->in;
62+
unsigned const char *q = strm->next_in;
6263
unsigned n = strm->avail_in;
6364
do {
6465
*p++ = *q++;

gzwrite.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ int ZEXPORT gzwrite(file, buf, len)
168168
unsigned len;
169169
{
170170
unsigned put = len;
171-
unsigned n;
172171
gz_statep state;
173172
z_streamp strm;
174173

@@ -208,16 +207,19 @@ int ZEXPORT gzwrite(file, buf, len)
208207
if (len < state->size) {
209208
/* copy to input buffer, compress when full */
210209
do {
210+
unsigned have, copy;
211+
211212
if (strm->avail_in == 0)
212213
strm->next_in = state->in;
213-
n = state->size - strm->avail_in;
214-
if (n > len)
215-
n = len;
216-
memcpy(strm->next_in + strm->avail_in, buf, n);
217-
strm->avail_in += n;
218-
state->x.pos += n;
219-
buf = (char *)buf + n;
220-
len -= n;
214+
have = strm->next_in + strm->avail_in - state->in;
215+
copy = state->size - have;
216+
if (copy > len)
217+
copy = len;
218+
memcpy(state->in + have, buf, copy);
219+
strm->avail_in += copy;
220+
state->x.pos += copy;
221+
buf = (const char *)buf + copy;
222+
len -= copy;
221223
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
222224
return 0;
223225
} while (len);
@@ -229,7 +231,7 @@ int ZEXPORT gzwrite(file, buf, len)
229231

230232
/* directly compress user buffer to file */
231233
strm->avail_in = len;
232-
strm->next_in = (voidp)buf;
234+
strm->next_in = (z_const Bytef *)buf;
233235
state->x.pos += len;
234236
if (gz_comp(state, Z_NO_FLUSH) == -1)
235237
return 0;
@@ -244,6 +246,7 @@ int ZEXPORT gzputc(file, c)
244246
gzFile file;
245247
int c;
246248
{
249+
unsigned have;
247250
unsigned char buf[1];
248251
gz_statep state;
249252
z_streamp strm;
@@ -267,10 +270,12 @@ int ZEXPORT gzputc(file, c)
267270

268271
/* try writing to input buffer for speed (state->size == 0 if buffer not
269272
initialized) */
270-
if (strm->avail_in < state->size) {
271-
if (strm->avail_in == 0)
272-
strm->next_in = state->in;
273-
strm->next_in[strm->avail_in++] = c;
273+
if (strm->avail_in == 0)
274+
strm->next_in = state->in;
275+
have = strm->next_in + strm->avail_in - state->in;
276+
if (have < state->size) {
277+
state->in[have] = c;
278+
strm->avail_in++;
274279
state->x.pos++;
275280
return c & 0xff;
276281
}

infback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ out_func out;
255255
void FAR *out_desc;
256256
{
257257
struct inflate_state FAR *state;
258-
unsigned char FAR *next; /* next input */
258+
z_const unsigned char FAR *next; /* next input */
259259
unsigned char FAR *put; /* next output */
260260
unsigned have, left; /* available input and output */
261261
unsigned long hold; /* bit buffer */

inffast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ z_streamp strm;
6969
unsigned start; /* inflate()'s starting value for strm->avail_out */
7070
{
7171
struct inflate_state FAR *state;
72-
unsigned char FAR *in; /* local strm->next_in */
73-
unsigned char FAR *last; /* while in < last, enough input available */
72+
z_const unsigned char FAR *in; /* local strm->next_in */
73+
z_const unsigned char FAR *last; /* while in < last, enough input available */
7474
unsigned char FAR *out; /* local strm->next_out */
7575
unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
7676
unsigned char FAR *end; /* while out < end, enough space available */

inflate.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@
9393

9494
/* function prototypes */
9595
local void fixedtables OF((struct inflate_state FAR *state));
96-
local int updatewindow OF((z_streamp strm, unsigned out));
96+
local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
97+
unsigned copy));
9798
#ifdef BUILDFIXED
9899
void makefixed OF((void));
99100
#endif
100-
local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
101+
local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
101102
unsigned len));
102103

103104
int ZEXPORT inflateResetKeep(strm)
@@ -375,12 +376,13 @@ void makefixed()
375376
output will fall in the output data, making match copies simpler and faster.
376377
The advantage may be dependent on the size of the processor's data caches.
377378
*/
378-
local int updatewindow(strm, out)
379+
local int updatewindow(strm, end, copy)
379380
z_streamp strm;
380-
unsigned out;
381+
const Bytef *end;
382+
unsigned copy;
381383
{
382384
struct inflate_state FAR *state;
383-
unsigned copy, dist;
385+
unsigned dist;
384386

385387
state = (struct inflate_state FAR *)strm->state;
386388

@@ -400,19 +402,18 @@ unsigned out;
400402
}
401403

402404
/* copy state->wsize or less output bytes into the circular window */
403-
copy = out - strm->avail_out;
404405
if (copy >= state->wsize) {
405-
zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
406+
zmemcpy(state->window, end - state->wsize, state->wsize);
406407
state->wnext = 0;
407408
state->whave = state->wsize;
408409
}
409410
else {
410411
dist = state->wsize - state->wnext;
411412
if (dist > copy) dist = copy;
412-
zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
413+
zmemcpy(state->window + state->wnext, end - copy, dist);
413414
copy -= dist;
414415
if (copy) {
415-
zmemcpy(state->window, strm->next_out - copy, copy);
416+
zmemcpy(state->window, end - copy, copy);
416417
state->wnext = copy;
417418
state->whave = state->wsize;
418419
}
@@ -606,7 +607,7 @@ z_streamp strm;
606607
int flush;
607608
{
608609
struct inflate_state FAR *state;
609-
unsigned char FAR *next; /* next input */
610+
z_const unsigned char FAR *next; /* next input */
610611
unsigned char FAR *put; /* next output */
611612
unsigned have, left; /* available input and output */
612613
unsigned long hold; /* bit buffer */
@@ -920,7 +921,7 @@ int flush;
920921
while (state->have < 19)
921922
state->lens[order[state->have++]] = 0;
922923
state->next = state->codes;
923-
state->lencode = (code const FAR *)(state->next);
924+
state->lencode = (const code FAR *)(state->next);
924925
state->lenbits = 7;
925926
ret = inflate_table(CODES, state->lens, 19, &(state->next),
926927
&(state->lenbits), state->work);
@@ -994,7 +995,7 @@ int flush;
994995
values here (9 and 6) without reading the comments in inftrees.h
995996
concerning the ENOUGH constants, which depend on those values */
996997
state->next = state->codes;
997-
state->lencode = (code const FAR *)(state->next);
998+
state->lencode = (const code FAR *)(state->next);
998999
state->lenbits = 9;
9991000
ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
10001001
&(state->lenbits), state->work);
@@ -1003,7 +1004,7 @@ int flush;
10031004
state->mode = BAD;
10041005
break;
10051006
}
1006-
state->distcode = (code const FAR *)(state->next);
1007+
state->distcode = (const code FAR *)(state->next);
10071008
state->distbits = 6;
10081009
ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
10091010
&(state->next), &(state->distbits), state->work);
@@ -1230,7 +1231,7 @@ int flush;
12301231
RESTORE();
12311232
if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
12321233
(state->mode < CHECK || flush != Z_FINISH)))
1233-
if (updatewindow(strm, out)) {
1234+
if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
12341235
state->mode = MEM;
12351236
return Z_MEM_ERROR;
12361237
}
@@ -1294,8 +1295,6 @@ uInt dictLength;
12941295
{
12951296
struct inflate_state FAR *state;
12961297
unsigned long dictid;
1297-
unsigned char *next;
1298-
unsigned avail;
12991298
int ret;
13001299

13011300
/* check state */
@@ -1314,13 +1313,7 @@ uInt dictLength;
13141313

13151314
/* copy dictionary to window using updatewindow(), which will amend the
13161315
existing dictionary if appropriate */
1317-
next = strm->next_out;
1318-
avail = strm->avail_out;
1319-
strm->next_out = (Bytef *)dictionary + dictLength;
1320-
strm->avail_out = 0;
1321-
ret = updatewindow(strm, dictLength);
1322-
strm->avail_out = avail;
1323-
strm->next_out = next;
1316+
ret = updatewindow(strm, dictionary + dictLength, dictLength);
13241317
if (ret) {
13251318
state->mode = MEM;
13261319
return Z_MEM_ERROR;
@@ -1360,7 +1353,7 @@ gz_headerp head;
13601353
*/
13611354
local unsigned syncsearch(have, buf, len)
13621355
unsigned FAR *have;
1363-
unsigned char FAR *buf;
1356+
const unsigned char FAR *buf;
13641357
unsigned len;
13651358
{
13661359
unsigned got;

test/example.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
} \
2727
}
2828

29-
const char hello[] = "hello, hello!";
29+
z_const char hello[] = "hello, hello!";
3030
/* "hello world" would be more standard, but the repeated "hello"
3131
* stresses the compression code better, sorry...
3232
*/
@@ -212,7 +212,7 @@ void test_deflate(compr, comprLen)
212212
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
213213
CHECK_ERR(err, "deflateInit");
214214

215-
c_stream.next_in = (Bytef*)hello;
215+
c_stream.next_in = (z_const unsigned char *)hello;
216216
c_stream.next_out = compr;
217217

218218
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
@@ -387,7 +387,7 @@ void test_flush(compr, comprLen)
387387
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
388388
CHECK_ERR(err, "deflateInit");
389389

390-
c_stream.next_in = (Bytef*)hello;
390+
c_stream.next_in = (z_const unsigned char *)hello;
391391
c_stream.next_out = compr;
392392
c_stream.avail_in = 3;
393393
c_stream.avail_out = (uInt)*comprLen;
@@ -476,7 +476,7 @@ void test_dict_deflate(compr, comprLen)
476476
c_stream.next_out = compr;
477477
c_stream.avail_out = (uInt)comprLen;
478478

479-
c_stream.next_in = (Bytef*)hello;
479+
c_stream.next_in = (z_const unsigned char *)hello;
480480
c_stream.avail_in = (uInt)strlen(hello)+1;
481481

482482
err = deflate(&c_stream, Z_FINISH);

0 commit comments

Comments
 (0)