]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Add various Assume hints hint-input-ranges 1808/head
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 5 Nov 2024 12:20:11 +0000 (13:20 +0100)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 11 Feb 2025 22:21:34 +0000 (23:21 +0100)
deflate_p.h
match_tpl.h
trees.c
trees_emit.h
zbuild.h

index cda681b055bead99441bf3399dfe874cbaef96f6..9e1ac182fea0cdca531775f4c94cda49ec8a059f 100644 (file)
@@ -88,7 +88,7 @@ static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t le
 #endif
     s->matches++;
     dist--;
-    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
+    AssertHint(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
         "zng_tr_tally: bad match");
 
     s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
index 47e9aed9f55ebd12eea1dce3a6a1dd7848533013..94f69547c6128fdf18e08b5d73a866ae209c402d 100644 (file)
@@ -115,7 +115,7 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
 #else
     early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
 #endif
-    Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
+    AssertHint((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
     for (;;) {
         if (cur_match >= strstart)
             break;
diff --git a/trees.c b/trees.c
index 49be6c48a11b57092358dfd7458a6e0cb6540688..7aa3b717b19b66c598d9115a8f927d413633b3f4 100644 (file)
--- a/trees.c
+++ b/trees.c
@@ -80,6 +80,7 @@ static int  detect_data_type (deflate_state *s);
  * Initialize the tree data structures for a new zlib stream.
  */
 void Z_INTERNAL zng_tr_init(deflate_state *s) {
+    Assume(s != NULL);
     s->l_desc.dyn_tree = s->dyn_ltree;
     s->l_desc.stat_desc = &static_l_desc;
 
@@ -104,6 +105,7 @@ void Z_INTERNAL zng_tr_init(deflate_state *s) {
  * Initialize a new block.
  */
 static void init_block(deflate_state *s) {
+    Assume(s != NULL);
     int n; /* iterates over tree elements */
 
     /* Initialize the trees. */
@@ -151,6 +153,7 @@ static void init_block(deflate_state *s) {
 static void pqdownheap(deflate_state *s, ct_data *tree, int k) {
     /* tree: the tree to restore */
     /* k: node to move down */
+    Assume(s != NULL && tree != NULL);
     int v = s->heap[k];
     int j = k << 1;  /* left son of k */
     while (j <= s->heap_len) {
@@ -184,6 +187,7 @@ static void pqdownheap(deflate_state *s, ct_data *tree, int k) {
  */
 static void gen_bitlen(deflate_state *s, tree_desc *desc) {
     /* desc: the tree descriptor */
+    Assume(s != NULL && desc != NULL);
     ct_data *tree           = desc->dyn_tree;
     int max_code            = desc->max_code;
     const ct_data *stree    = desc->stat_desc->static_tree;
@@ -321,6 +325,7 @@ Z_INTERNAL void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
  */
 static void build_tree(deflate_state *s, tree_desc *desc) {
     /* desc: the tree descriptor */
+    Assume(s != NULL && desc != NULL);
     ct_data *tree         = desc->dyn_tree;
     const ct_data *stree  = desc->stat_desc->static_tree;
     int elems             = desc->stat_desc->elems;
@@ -411,6 +416,7 @@ static void build_tree(deflate_state *s, tree_desc *desc) {
 static void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
     /* tree: the tree to be scanned */
     /* max_code: and its largest code of non zero frequency */
+    Assume(s != NULL && tree != NULL);
     int n;                     /* iterates over all tree elements */
     int prevlen = -1;          /* last emitted length */
     int curlen;                /* length of current code */
@@ -459,6 +465,8 @@ static void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
 static void send_tree(deflate_state *s, ct_data *tree, int max_code) {
     /* tree: the tree to be scanned */
     /* max_code and its largest code of non zero frequency */
+    Assume(s != NULL && tree != NULL);
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
     int n;                     /* iterates over all tree elements */
     int prevlen = -1;          /* last emitted length */
     int curlen;                /* length of current code */
@@ -523,6 +531,7 @@ static void send_tree(deflate_state *s, ct_data *tree, int max_code) {
  * bl_order of the last bit length code to send.
  */
 static int build_bl_tree(deflate_state *s) {
+    Assume(s != NULL);
     int max_blindex;  /* index of last bit length code of non zero freq */
 
     /* Determine the bit length frequencies for literal and distance trees */
@@ -556,6 +565,8 @@ static int build_bl_tree(deflate_state *s) {
  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  */
 static void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes) {
+    Assume(s != NULL);
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
     int rank;                    /* index in bl_order */
 
     AssertHint(lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
@@ -593,6 +604,8 @@ void Z_INTERNAL zng_tr_stored_block(deflate_state *s, char *buf, uint32_t stored
     /* buf: input block */
     /* stored_len: length of input block */
     /* last: one if this is the last block for a file */
+    Assume(s != NULL);
+
     zng_tr_emit_tree(s, STORED_BLOCK, last); /* send block type */
     zng_tr_emit_align(s);                    /* align on byte boundary */
     cmpr_bits_align(s);
@@ -613,6 +626,7 @@ void Z_INTERNAL zng_tr_stored_block(deflate_state *s, char *buf, uint32_t stored
  * This takes 10 bits, of which 7 may remain in the bit buffer.
  */
 void Z_INTERNAL zng_tr_align(deflate_state *s) {
+    Assume(s != NULL);
     zng_tr_emit_tree(s, STATIC_TREES, 0);
     zng_tr_emit_end_block(s, static_ltree, 0);
     zng_tr_flush_bits(s);
@@ -626,6 +640,8 @@ void Z_INTERNAL zng_tr_flush_block(deflate_state *s, char *buf, uint32_t stored_
     /* buf: input block, or NULL if too old */
     /* stored_len: length of input block */
     /* last: one if this is the last block for a file */
+    Assume(s != NULL);
+
     unsigned long opt_lenb, static_lenb; /* opt_len and static_len in bytes */
     int max_blindex = 0;  /* index of last bit length code of non zero freq */
 
@@ -666,7 +682,7 @@ void Z_INTERNAL zng_tr_flush_block(deflate_state *s, char *buf, uint32_t stored_
             opt_lenb = static_lenb;
 
     } else {
-        Assert(buf != NULL, "lost buf");
+        AssertHint(buf != NULL, "lost buf");
         opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
     }
 
@@ -708,6 +724,8 @@ void Z_INTERNAL zng_tr_flush_block(deflate_state *s, char *buf, uint32_t stored_
 static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree) {
     /* ltree: literal tree */
     /* dtree: distance tree */
+    Assume(s != NULL && ltree != NULL && dtree != NULL);
+
     unsigned dist;      /* distance of matched string */
     int lc;             /* match length or unmatched char (if dist == 0) */
     unsigned sx = 0;    /* running index in symbol buffers */
@@ -754,6 +772,7 @@ static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data
  * IN assertion: the fields Freq of dyn_ltree are set.
  */
 static int detect_data_type(deflate_state *s) {
+    Assume(s != NULL);
     /* black_mask is the bit mask of black-listed bytes
      * set bits 0..6, 14..25, and 28..31
      * 0xf3ffc07f = binary 11110011111111111100000001111111
@@ -783,6 +802,8 @@ static int detect_data_type(deflate_state *s) {
  * Flush the bit buffer, keeping at most 7 bits in it.
  */
 void Z_INTERNAL zng_tr_flush_bits(deflate_state *s) {
+    Assume(s != NULL);
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
     if (s->bi_valid >= 48) {
         put_uint32(s, (uint32_t)s->bi_buf);
         put_short(s, (uint16_t)(s->bi_buf >> 32));
index 1c2bd5e758e26ddd83167096fcdc28cb0d219319..eed6601f1d3c0770e47e9b78683ac5379b08c16d 100644 (file)
@@ -44,6 +44,7 @@ extern Z_INTERNAL const int base_dist[D_CODES];
  *       otherwise the shifts will overflow.
  */
 #define send_bits(s, t_val, t_len, bi_buf, bi_valid) {\
+    Assume(bi_valid <= BIT_BUF_SIZE);\
     uint64_t val = (uint64_t)t_val;\
     uint32_t len = (uint32_t)t_len;\
     uint32_t total_bits = bi_valid + len;\
@@ -79,6 +80,8 @@ extern Z_INTERNAL const int base_dist[D_CODES];
  * Flush the bit buffer and align the output on a byte boundary
  */
 static void bi_windup(deflate_state *s) {
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
+
     if (s->bi_valid > 56) {
         put_uint64(s, s->bi_buf);
     } else {
@@ -104,6 +107,7 @@ static void bi_windup(deflate_state *s) {
  * Emit literal code
  */
 static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
     uint32_t bi_valid = s->bi_valid;
     uint64_t bi_buf = s->bi_buf;
 
@@ -122,6 +126,7 @@ static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsi
  */
 static inline uint32_t zng_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
     uint32_t lc, uint32_t dist) {
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
     uint32_t c, extra;
     uint8_t code;
     uint64_t match_bits;
@@ -171,6 +176,7 @@ static inline uint32_t zng_emit_dist(deflate_state *s, const ct_data *ltree, con
  * Emit end block
  */
 static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
     uint32_t bi_valid = s->bi_valid;
     uint64_t bi_buf = s->bi_buf;
     send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
@@ -200,6 +206,7 @@ static inline void zng_tr_emit_dist(deflate_state *s, const ct_data *ltree, cons
  * Emit start of block
  */
 static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
+    Assume(s->bi_valid <= BIT_BUF_SIZE);
     uint32_t bi_valid = s->bi_valid;
     uint64_t bi_buf = s->bi_buf;
     uint32_t header_bits = (type << 1) + last;
index 33a4058765f2d456700664510e46663c4fe36f17..7e0be216cc2d9d97dd188e3ab51e7bd5077f6983 100644 (file)
--- a/zbuild.h
+++ b/zbuild.h
 #  define Assume(cond)          Assert(cond, "Value assumption failed")
 #else
 #  define Assert(cond, msg)
-#  define AssertHint(cind, msg) z_assume(cond)
+#  define AssertHint(cond, msg) z_assume(cond)
 #  define Assume(cond)          z_assume(cond)
 #endif