]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Add Z_UNREACHABLE compiler hint
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 13 Jan 2026 14:32:53 +0000 (15:32 +0100)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 14 Jan 2026 13:08:37 +0000 (14:08 +0100)
deflate.c
gzread.c
zbuild.h

index aa534fb29702cd0030f1a488a2d66f00a1a03a97..a9645ce8e6eedc2493f837dac36fda71b6cc2d78 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -711,6 +711,7 @@ unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long
         break;
 #endif
     default:                                /* for compiler happiness */
+        Z_UNREACHABLE();
         wraplen = ZLIB_WRAPLEN;
     }
 
index d52fea80ece0e8b231335a245a61d0b3019e55ff..3e321a03bab64aa15f6ed811aeeca1720f63a651 100644 (file)
--- a/gzread.c
+++ b/gzread.c
@@ -232,6 +232,7 @@ static int gz_fetch(gz_state *state) {
                 return -1;
             continue;
         default:    // Can't happen
+            Z_UNREACHABLE();
             return -1;
         }
     } while (state->x.have == 0 && (!state->eof || strm->avail_in));
index 954bad316ca4517738e378ad88183bd2e360a856..b17362767f819b350cb5e88867d8f1b3d68fd280 100644 (file)
--- a/zbuild.h
+++ b/zbuild.h
 #  endif
 #endif
 
+/* Hint to compiler that a block of code is unreachable, typically in a switch default condition */
+#ifndef Z_UNREACHABLE
+#  if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
+#    define Z_UNREACHABLE() unreachable()           // C23 approach
+#  elif (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
+#    define Z_UNREACHABLE() __builtin_unreachable()
+#  else
+#    define Z_UNREACHABLE()
+#  endif
+#endif
+
 #ifndef Z_TARGET
 #  if Z_HAS_ATTRIBUTE(__target__)
 #    define Z_TARGET(x) __attribute__((__target__(x)))