Skip to content

Commit 70b3576

Browse files
committed
Fix INTEGER_OVERFLOW in gen_tables64.h
Implemented the same signed-midpoint fix in the 64-bit helpers (s2i_64__, i2s_64_bsearch__) by changing mid from size_t to ssize_t, preventing mid - 1 unsigned wraparound in binary search bound updates. Added the edge-case guard for empty tables (n == 0) before right = n - 1 in both 32-bit and 64-bit search helpers, avoiding underflow when n is zero.
1 parent 98f32bf commit 70b3576

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/gen_tables.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* gen_tables.h -- Declarations used for lookup tables.
2-
* Copyright 2008 Red Hat Inc., Durham, North Carolina.
2+
* Copyright 2008 Red Hat Inc.
33
* All Rights Reserved.
44
*
55
* This library is free software; you can redistribute it and/or
@@ -33,6 +33,10 @@ inline static int s2i__(const char *strings, const unsigned *s_table,
3333
const int *i_table, size_t n, const char *s, int *value)
3434
{
3535
size_t lo = 0, hi = 0;
36+
37+
if (n == 0)
38+
return 0;
39+
3640
ssize_t left = 0, right = n - 1;
3741

3842
while (left <= right) { /* invariant: left <= x <= right */
@@ -85,6 +89,9 @@ inline static const char *i2s_bsearch__(const char *strings,
8589
{
8690
ssize_t left, right;
8791

92+
if (n == 0)
93+
return NULL;
94+
8895
left = 0;
8996
right = n - 1;
9097
while (left <= right) { /* invariant: left <= x <= right */

lib/gen_tables64.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ inline static int s2i_64__(const char *strings, const unsigned *s_table,
4848
const int64_t *i_table, size_t n, const char *s, int64_t *value)
4949
{
5050
size_t lo = 0, hi = 0;
51+
if (n == 0)
52+
return 0;
53+
5154
ssize_t left = 0, right = n - 1;
5255

5356
while (left <= right) { /* invariant: left <= x <= right */
54-
size_t mid, off, i;
57+
ssize_t mid;
58+
size_t off, i;
5559
const char *t;
5660
int r;
5761

@@ -103,10 +107,13 @@ inline static const char *i2s_64_bsearch__(const char *strings,
103107
{
104108
ssize_t left, right;
105109

110+
if (n == 0)
111+
return NULL;
112+
106113
left = 0;
107114
right = n - 1;
108115
while (left <= right) { /* invariant: left <= x <= right */
109-
size_t mid;
116+
ssize_t mid;
110117
int64_t mid_val;
111118

112119
mid = (left + right) / 2;

0 commit comments

Comments
 (0)