zlib: annotate switch/case fallthrough cases

The inflate state machine in zlib uses switch/case fall-through's
extensively, as it sometimes advances the state, and lets the
conveniently placed next case statement handle the new state already.
The pattern here is:
		state->mode = LEN;
	case LEN:

Annotate those occasions with the "fallthrough;" macro, to let compilers
know this is fine when using -Wimplicit-fallthrough.

This mimics the upstream commit 76f70abbc73f:
Author:  Mark Adler <madler@alumni.caltech.edu>
Date:    Sun Mar 27 00:12:38 2022 -0700
Subject: Add fallthrough comments for gcc.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Link: https://github.com/madler/zlib/commit/76f70abbc73f
Reviewed-by: Tom Rini <trini@konsulko.com>
This commit is contained in:
Andre Przywara
2025-03-27 15:32:57 +00:00
committed by Tom Rini
parent a6a9d32733
commit 3d907a5a49

View File

@@ -420,6 +420,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
if (state->flags & 0x0200) CRC2(state->check, hold); if (state->flags & 0x0200) CRC2(state->check, hold);
INITBITS(); INITBITS();
state->mode = TIME; state->mode = TIME;
fallthrough;
case TIME: case TIME:
NEEDBITS(32); NEEDBITS(32);
if (state->head != Z_NULL) if (state->head != Z_NULL)
@@ -427,6 +428,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
if (state->flags & 0x0200) CRC4(state->check, hold); if (state->flags & 0x0200) CRC4(state->check, hold);
INITBITS(); INITBITS();
state->mode = OS; state->mode = OS;
fallthrough;
case OS: case OS:
NEEDBITS(16); NEEDBITS(16);
if (state->head != Z_NULL) { if (state->head != Z_NULL) {
@@ -436,6 +438,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
if (state->flags & 0x0200) CRC2(state->check, hold); if (state->flags & 0x0200) CRC2(state->check, hold);
INITBITS(); INITBITS();
state->mode = EXLEN; state->mode = EXLEN;
fallthrough;
case EXLEN: case EXLEN:
if (state->flags & 0x0400) { if (state->flags & 0x0400) {
NEEDBITS(16); NEEDBITS(16);
@@ -448,6 +451,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
else if (state->head != Z_NULL) else if (state->head != Z_NULL)
state->head->extra = Z_NULL; state->head->extra = Z_NULL;
state->mode = EXTRA; state->mode = EXTRA;
fallthrough;
case EXTRA: case EXTRA:
if (state->flags & 0x0400) { if (state->flags & 0x0400) {
copy = state->length; copy = state->length;
@@ -471,6 +475,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
state->length = 0; state->length = 0;
state->mode = NAME; state->mode = NAME;
fallthrough;
case NAME: case NAME:
if (state->flags & 0x0800) { if (state->flags & 0x0800) {
if (have == 0) goto inf_leave; if (have == 0) goto inf_leave;
@@ -492,6 +497,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
state->head->name = Z_NULL; state->head->name = Z_NULL;
state->length = 0; state->length = 0;
state->mode = COMMENT; state->mode = COMMENT;
fallthrough;
case COMMENT: case COMMENT:
if (state->flags & 0x1000) { if (state->flags & 0x1000) {
if (have == 0) goto inf_leave; if (have == 0) goto inf_leave;
@@ -512,6 +518,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
else if (state->head != Z_NULL) else if (state->head != Z_NULL)
state->head->comment = Z_NULL; state->head->comment = Z_NULL;
state->mode = HCRC; state->mode = HCRC;
fallthrough;
case HCRC: case HCRC:
if (state->flags & 0x0200) { if (state->flags & 0x0200) {
NEEDBITS(16); NEEDBITS(16);
@@ -535,6 +542,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
strm->adler = state->check = REVERSE(hold); strm->adler = state->check = REVERSE(hold);
INITBITS(); INITBITS();
state->mode = DICT; state->mode = DICT;
fallthrough;
case DICT: case DICT:
if (state->havedict == 0) { if (state->havedict == 0) {
RESTORE(); RESTORE();
@@ -542,9 +550,11 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
strm->adler = state->check = adler32(0L, Z_NULL, 0); strm->adler = state->check = adler32(0L, Z_NULL, 0);
state->mode = TYPE; state->mode = TYPE;
fallthrough;
case TYPE: case TYPE:
schedule(); schedule();
if (flush == Z_BLOCK) goto inf_leave; if (flush == Z_BLOCK) goto inf_leave;
fallthrough;
case TYPEDO: case TYPEDO:
if (state->last) { if (state->last) {
BYTEBITS(); BYTEBITS();
@@ -590,6 +600,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
state->length)); state->length));
INITBITS(); INITBITS();
state->mode = COPY; state->mode = COPY;
fallthrough;
case COPY: case COPY:
copy = state->length; copy = state->length;
if (copy) { if (copy) {
@@ -625,6 +636,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
Tracev((stderr, "inflate: table sizes ok\n")); Tracev((stderr, "inflate: table sizes ok\n"));
state->have = 0; state->have = 0;
state->mode = LENLENS; state->mode = LENLENS;
fallthrough;
case LENLENS: case LENLENS:
while (state->have < state->ncode) { while (state->have < state->ncode) {
NEEDBITS(3); NEEDBITS(3);
@@ -646,6 +658,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
Tracev((stderr, "inflate: code lengths ok\n")); Tracev((stderr, "inflate: code lengths ok\n"));
state->have = 0; state->have = 0;
state->mode = CODELENS; state->mode = CODELENS;
fallthrough;
case CODELENS: case CODELENS:
while (state->have < state->nlen + state->ndist) { while (state->have < state->nlen + state->ndist) {
for (;;) { for (;;) {
@@ -720,6 +733,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
Tracev((stderr, "inflate: codes ok\n")); Tracev((stderr, "inflate: codes ok\n"));
state->mode = LEN; state->mode = LEN;
fallthrough;
case LEN: case LEN:
schedule(); schedule();
if (have >= 6 && left >= 258) { if (have >= 6 && left >= 258) {
@@ -764,6 +778,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
state->extra = (unsigned)(this.op) & 15; state->extra = (unsigned)(this.op) & 15;
state->mode = LENEXT; state->mode = LENEXT;
fallthrough;
case LENEXT: case LENEXT:
if (state->extra) { if (state->extra) {
NEEDBITS(state->extra); NEEDBITS(state->extra);
@@ -772,6 +787,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
Tracevv((stderr, "inflate: length %u\n", state->length)); Tracevv((stderr, "inflate: length %u\n", state->length));
state->mode = DIST; state->mode = DIST;
fallthrough;
case DIST: case DIST:
for (;;) { for (;;) {
this = state->distcode[BITS(state->distbits)]; this = state->distcode[BITS(state->distbits)];
@@ -797,6 +813,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
state->offset = (unsigned)this.val; state->offset = (unsigned)this.val;
state->extra = (unsigned)(this.op) & 15; state->extra = (unsigned)(this.op) & 15;
state->mode = DISTEXT; state->mode = DISTEXT;
fallthrough;
case DISTEXT: case DISTEXT:
if (state->extra) { if (state->extra) {
NEEDBITS(state->extra); NEEDBITS(state->extra);
@@ -817,6 +834,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
Tracevv((stderr, "inflate: distance %u\n", state->offset)); Tracevv((stderr, "inflate: distance %u\n", state->offset));
state->mode = MATCH; state->mode = MATCH;
fallthrough;
case MATCH: case MATCH:
if (left == 0) goto inf_leave; if (left == 0) goto inf_leave;
copy = out - left; copy = out - left;
@@ -872,6 +890,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
#ifdef GUNZIP #ifdef GUNZIP
state->mode = LENGTH; state->mode = LENGTH;
fallthrough;
case LENGTH: case LENGTH:
if (state->wrap && state->flags) { if (state->wrap && state->flags) {
NEEDBITS(32); NEEDBITS(32);
@@ -885,6 +904,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
} }
#endif #endif
state->mode = DONE; state->mode = DONE;
fallthrough;
case DONE: case DONE:
ret = Z_STREAM_END; ret = Z_STREAM_END;
goto inf_leave; goto inf_leave;
@@ -894,6 +914,7 @@ __rcode int ZEXPORT inflate(z_streamp strm, int flush)
case MEM: case MEM:
return Z_MEM_ERROR; return Z_MEM_ERROR;
case SYNC: case SYNC:
fallthrough;
default: default:
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
} }