[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints
From: |
Robbie Harwood |
Subject: |
[PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints |
Date: |
Wed, 1 Dec 2021 16:02:03 -0500 |
From: Peter Jones <pjones@redhat.com>
[rharwood@redhat.com: rebase conflicts in regexec, regcomp]
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
lib/argp-fmtstream.c | 2 +-
lib/regcomp.c | 28 +++++++++++++++-------------
lib/regex_internal.c | 6 +++---
lib/regexec.c | 36 ++++++++++++++++++++----------------
lib/vasnprintf.c | 4 ++--
5 files changed, 41 insertions(+), 35 deletions(-)
diff --git a/lib/argp-fmtstream.c b/lib/argp-fmtstream.c
index f679751b9..4aa401e2d 100644
--- a/lib/argp-fmtstream.c
+++ b/lib/argp-fmtstream.c
@@ -234,7 +234,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
else
{
size_t display_width = mbsnwidth (buf, nl - buf, MBSW_STOP_AT_NUL);
- if (display_width < (ssize_t) fs->rmargin)
+ if (display_width < fs->rmargin)
{
/* The buffer contains a full line that fits within the maximum
line width. Reset point and scan the next line. */
diff --git a/lib/regcomp.c b/lib/regcomp.c
index a8cefebf1..b2ab8f9a3 100644
--- a/lib/regcomp.c
+++ b/lib/regcomp.c
@@ -286,7 +286,7 @@ re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t
*init_state,
bool icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
{
- Idx node = init_state->nodes.elems[node_cnt];
+ size_t node = init_state->nodes.elems[node_cnt];
re_token_type_t type = dfa->nodes[node].type;
if (type == CHARACTER)
@@ -306,7 +306,7 @@ re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t
*init_state,
&& dfa->nodes[node].mb_partial)
*p++ = dfa->nodes[node].opr.c;
memset (&state, '\0', sizeof (state));
- if (__mbrtowc (&wc, (const char *) buf, p - buf,
+ if ((ssize_t)__mbrtowc (&wc, (const char *) buf, p - buf,
&state) == p - buf
&& (__wcrtomb ((char *) buf, __towlower (wc), &state)
!= (size_t) -1))
@@ -558,7 +558,8 @@ static const bitset_t utf8_sb_map =
static void
free_dfa_content (re_dfa_t *dfa)
{
- Idx i, j;
+ size_t i;
+ Idx j;
if (dfa->nodes)
for (i = 0; i < dfa->nodes_len; ++i)
@@ -860,7 +861,8 @@ init_dfa (re_dfa_t *dfa, size_t pat_len)
dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map;
else
{
- int i, j, ch;
+ int i, j;
+ wint_t ch;
dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
if (__glibc_unlikely (dfa->sb_char == NULL))
@@ -1045,7 +1047,7 @@ create_initial_state (re_dfa_t *dfa)
static void
optimize_utf8 (re_dfa_t *dfa)
{
- Idx node;
+ size_t node;
int i;
bool mb_chars = false;
bool has_period = false;
@@ -1139,12 +1141,12 @@ analyze (regex_t *preg)
dfa->subexp_map = re_malloc (Idx, preg->re_nsub);
if (dfa->subexp_map != NULL)
{
- Idx i;
+ size_t i;
for (i = 0; i < preg->re_nsub; i++)
dfa->subexp_map[i] = i;
preorder (dfa->str_tree, optimize_subexps, dfa);
for (i = 0; i < preg->re_nsub; i++)
- if (dfa->subexp_map[i] != i)
+ if (dfa->subexp_map[i] != (ssize_t)i)
break;
if (i == preg->re_nsub)
{
@@ -1589,7 +1591,7 @@ duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int
constraint)
static reg_errcode_t
calc_inveclosure (re_dfa_t *dfa)
{
- Idx src, idx;
+ size_t src, idx;
bool ok;
for (idx = 0; idx < dfa->nodes_len; ++idx)
re_node_set_init_empty (dfa->inveclosures + idx);
@@ -1597,7 +1599,7 @@ calc_inveclosure (re_dfa_t *dfa)
for (src = 0; src < dfa->nodes_len; ++src)
{
Idx *elems = dfa->eclosures[src].elems;
- for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
+ for (idx = 0; (ssize_t)idx < dfa->eclosures[src].nelem; ++idx)
{
ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
if (__glibc_unlikely (! ok))
@@ -1613,7 +1615,7 @@ calc_inveclosure (re_dfa_t *dfa)
static reg_errcode_t
calc_eclosure (re_dfa_t *dfa)
{
- Idx node_idx;
+ size_t node_idx;
bool incomplete;
DEBUG_ASSERT (dfa->nodes_len > 0);
incomplete = false;
@@ -2666,9 +2668,9 @@ build_range_exp (bitset_t sbcset, re_charset_t *mbcset,
Idx *range_alloc,
: 0));
wint_t
start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
- ? parse_byte (start_ch, dfa) : start_elem->opr.wch),
+ ? parse_byte (start_ch, dfa) : (wint_t) start_elem->opr.wch),
end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
- ? parse_byte (end_ch, dfa) : end_elem->opr.wch);
+ ? parse_byte (end_ch, dfa) : (wint_t) end_elem->opr.wch);
if (start_wc == WEOF || end_wc == WEOF)
return REG_ECOLLATE;
@@ -2717,7 +2719,7 @@ build_range_exp (bitset_t sbcset, re_charset_t *mbcset,
Idx *range_alloc,
}
/* Build the table for single byte characters. */
- for (wchar_t wc = 0; wc < SBC_MAX; ++wc)
+ for (wint_t wc = 0; wc < SBC_MAX; ++wc)
{
if (start_wc <= wc && wc <= end_wc)
bitset_set (sbcset, wc);
diff --git a/lib/regex_internal.c b/lib/regex_internal.c
index 9767cd0d0..39d4f9cc1 100644
--- a/lib/regex_internal.c
+++ b/lib/regex_internal.c
@@ -139,7 +139,7 @@ re_string_realloc_buffers (re_string_t *pstr, Idx
new_buf_len)
/* Avoid overflow in realloc. */
const size_t max_object_size = MAX (sizeof (wint_t), sizeof (Idx));
- if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size)
+ if (__glibc_unlikely ((ssize_t)MIN (IDX_MAX, SIZE_MAX / max_object_size)
< new_buf_len))
return REG_ESPACE;
@@ -394,7 +394,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
{
size_t i;
- if (byte_idx + mbcdlen > pstr->bufs_len)
+ if ((ssize_t)(byte_idx + mbcdlen) > pstr->bufs_len)
{
pstr->cur_state = prev_st;
break;
@@ -734,7 +734,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int
eflags)
memset (&cur_state, 0, sizeof (cur_state));
mbclen = __mbrtowc (&wc2, (const char *) pp, mlen,
&cur_state);
- if (raw + offset - p <= mbclen
+ if (raw + offset - p <= (ssize_t)mbclen
&& mbclen < (size_t) -2)
{
memset (&pstr->cur_state, '\0',
diff --git a/lib/regexec.c b/lib/regexec.c
index 1aad68175..a9b4a9a4e 100644
--- a/lib/regexec.c
+++ b/lib/regexec.c
@@ -34,7 +34,7 @@ static void sift_ctx_init (re_sift_context_t *sctx,
re_dfastate_t **sifted_sts,
static reg_errcode_t re_search_internal (const regex_t *preg,
const char *string, Idx length,
Idx start, Idx last_start, Idx stop,
- size_t nmatch, regmatch_t pmatch[],
+ ssize_t nmatch, regmatch_t pmatch[],
int eflags);
static regoff_t re_search_2_stub (struct re_pattern_buffer *bufp,
const char *string1, Idx length1,
@@ -63,7 +63,7 @@ static reg_errcode_t push_fail_stack (struct re_fail_stack_t
*fs,
re_node_set *eps_via_nodes);
static reg_errcode_t set_regs (const regex_t *preg,
const re_match_context_t *mctx,
- size_t nmatch, regmatch_t *pmatch,
+ ssize_t nmatch, regmatch_t *pmatch,
bool fl_backtrack);
static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs);
@@ -478,7 +478,7 @@ re_copy_regs (struct re_registers *regs, regmatch_t
*pmatch, Idx nregs,
{ /* Yes. If we need more elements than were already
allocated, reallocate them. If we need fewer, just
leave it alone. */
- if (__glibc_unlikely (need_regs > regs->num_regs))
+ if (__glibc_unlikely (need_regs > (ssize_t)regs->num_regs))
{
regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
regoff_t *new_end;
@@ -499,7 +499,7 @@ re_copy_regs (struct re_registers *regs, regmatch_t
*pmatch, Idx nregs,
{
DEBUG_ASSERT (regs_allocated == REGS_FIXED);
/* This function may not be called with REGS_FIXED and nregs too big. */
- DEBUG_ASSERT (nregs <= regs->num_regs);
+ DEBUG_ASSERT ((ssize_t)nregs <= regs->num_regs);
rval = REGS_FIXED;
}
@@ -509,7 +509,7 @@ re_copy_regs (struct re_registers *regs, regmatch_t
*pmatch, Idx nregs,
regs->start[i] = pmatch[i].rm_so;
regs->end[i] = pmatch[i].rm_eo;
}
- for ( ; i < regs->num_regs; ++i)
+ for ( ; i < (ssize_t)regs->num_regs; ++i)
regs->start[i] = regs->end[i] = -1;
return rval;
@@ -578,7 +578,7 @@ re_exec (const char *s)
static reg_errcode_t
__attribute_warn_unused_result__
re_search_internal (const regex_t *preg, const char *string, Idx length,
- Idx start, Idx last_start, Idx stop, size_t nmatch,
+ Idx start, Idx last_start, Idx stop, ssize_t nmatch,
regmatch_t pmatch[], int eflags)
{
reg_errcode_t err;
@@ -598,7 +598,7 @@ re_search_internal (const regex_t *preg, const char
*string, Idx length,
? preg->fastmap : NULL);
RE_TRANSLATE_TYPE t = preg->translate;
- extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
+ extra_nmatch = (nmatch > (ssize_t)preg->re_nsub) ? nmatch - (preg->re_nsub +
1) : 0;
nmatch -= extra_nmatch;
/* Check if the DFA haven't been compiled. */
@@ -647,9 +647,10 @@ re_search_internal (const regex_t *preg, const char
*string, Idx length,
if (nmatch > 1 || dfa->has_mb_node)
{
/* Avoid overflow. */
- if (__glibc_unlikely ((MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
- <= mctx.input.bufs_len)))
- {
+ if (__glibc_unlikely ((ssize_t)MIN (IDX_MAX,
+ SIZE_MAX / sizeof (re_dfastate_t *))
+ <= mctx.input.bufs_len))
+ {
err = REG_ESPACE;
goto free_return;
}
@@ -908,7 +909,8 @@ prune_impossible_nodes (re_match_context_t *mctx)
halt_node = mctx->last_node;
/* Avoid overflow. */
- if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
+ if (__glibc_unlikely ((ssize_t)MIN (IDX_MAX,
+ SIZE_MAX / sizeof (re_dfastate_t *))
<= match_last))
return REG_ESPACE;
@@ -1365,7 +1367,7 @@ pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx,
Idx nregs,
static reg_errcode_t
__attribute_warn_unused_result__
-set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
+set_regs (const regex_t *preg, const re_match_context_t *mctx, ssize_t nmatch,
regmatch_t *pmatch, bool fl_backtrack)
{
const re_dfa_t *dfa = preg->buffer;
@@ -1400,7 +1402,7 @@ set_regs (const regex_t *preg, const re_match_context_t
*mctx, size_t nmatch,
regmatch_t *prev_idx_match = regmatch_list_begin (&prev_match);
memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
- for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
+ for (idx = pmatch[0].rm_so; (ssize_t)idx <= (long)pmatch[0].rm_eo ;)
{
update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
@@ -2835,7 +2837,8 @@ check_arrival (re_match_context_t *mctx, state_array_t
*path, Idx top_node,
if (__glibc_unlikely (IDX_MAX - old_alloc < incr_alloc))
return REG_ESPACE;
new_alloc = old_alloc + incr_alloc;
- if (__glibc_unlikely (SIZE_MAX / sizeof (re_dfastate_t *) < new_alloc))
+ if (__glibc_unlikely ((ssize_t)(SIZE_MAX / sizeof (re_dfastate_t *))
+ < new_alloc))
return REG_ESPACE;
new_array = re_realloc (path->array, re_dfastate_t *, new_alloc);
if (__glibc_unlikely (new_array == NULL))
@@ -3956,7 +3959,8 @@ extend_buffers (re_match_context_t *mctx, int min_len)
re_string_t *pstr = &mctx->input;
/* Avoid overflow. */
- if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *)) / 2
+ if (__glibc_unlikely ((ssize_t)MIN (IDX_MAX,
+ SIZE_MAX / sizeof (re_dfastate_t *)) / 2
<= pstr->bufs_len))
return REG_ESPACE;
@@ -4022,7 +4026,7 @@ match_ctx_init (re_match_context_t *mctx, int eflags, Idx
n)
size_t max_object_size =
MAX (sizeof (struct re_backref_cache_entry),
sizeof (re_sub_match_top_t *));
- if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size) < n))
+ if (__glibc_unlikely ((ssize_t)MIN (IDX_MAX, SIZE_MAX / max_object_size)
< n))
return REG_ESPACE;
mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index d9b669d15..360ca6948 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -5573,7 +5573,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
#endif
#if !USE_SNPRINTF
- if (count >= tmp_length)
+ if (count >= (ssize_t)tmp_length)
/* tmp_length was incorrectly calculated - fix the
code above! */
abort ();
@@ -5663,7 +5663,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
#if DCHAR_IS_TCHAR && !USE_SNPRINTF
/* Make room for the result. */
- if (count > allocated - length)
+ if (count > (ssize_t)(allocated - length))
{
/* Need at least count elements. But allocate
proportionally. */
--
2.33.0
- Re: [PATCH v2 02/10] gnulib/regexec: Fix possible null-dereference, (continued)
[PATCH v2 05/10] Make gnulib's regcomp not abort(), Robbie Harwood, 2021/12/01
[PATCH v2 06/10] Make CFLAGS less painful, Robbie Harwood, 2021/12/01
[PATCH v2 07/10] Fix __argp_fmtstream_point()'s return type and comparisons with it, Robbie Harwood, 2021/12/01
[PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints,
Robbie Harwood <=
- Re: [PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints, Paul Eggert, 2021/12/01
- Re: [PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints, Robbie Harwood, 2021/12/07
- Re: [PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints, Paul Eggert, 2021/12/07
- Re: [PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints, Bruno Haible, 2021/12/08
- Re: [PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints, Jim Meyering, 2021/12/08
- Re: [PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints, Paul Eggert, 2021/12/08
- Re: [PATCH v2 08/10] Fix up a bunch of "gcc -Werror=sign-compare" complaints, Robbie Harwood, 2021/12/09
[PATCH v2 09/10] Paper over a stringop-overflow warning about wide char handling, Robbie Harwood, 2021/12/01
[PATCH v2 10/10] Fixup for -Werror=ignored-qualifiers issues, Robbie Harwood, 2021/12/01
Re: [PATCH v2 00/10] Code hygiene fixes from grub, Bruno Haible, 2021/12/01