Discussion:
[PATCH] quoteargs: fix comparison between signed and unsigned warning
(too old to reply)
Sami Kerola
2017-10-19 19:42:07 UTC
Permalink
Raw Message
* quotearg.c: Avoid signed/unsigned comparison warning
---
lib/quotearg.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/quotearg.c b/lib/quotearg.c
index df602698e..1c90df301 100644
--- a/lib/quotearg.c
+++ b/lib/quotearg.c
@@ -888,7 +888,7 @@ quotearg_n_options (int n, char const *arg, size_t argsize,
{
bool preallocated = (sv == &slotvec0);

- if (MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) <= n)
+ if (MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) <= (size_t) n)
xalloc_die ();

slotvec = sv = xrealloc (preallocated ? NULL : sv, (n + 1) * sizeof *sv);
--
2.14.2
Paul Eggert
2017-10-19 20:05:51 UTC
Permalink
Raw Message
Post by Sami Kerola
- if (MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) <= n)
+ if (MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) <= (size_t) n)
Thanks for letting us know about the problem. I'd rather avoid casts
when possible, so I installed the attached patch instead.
Bruno Haible
2017-10-19 20:13:10 UTC
Permalink
Raw Message
Post by Sami Kerola
- if (MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) <= n)
+ if (MIN (INT_MAX, MIN (PTRDIFF_MAX, SIZE_MAX) / sizeof *sv) <= (size_t) n)
xalloc_die ();
I would find it better to cast n to 'unsigned int' instead. Then the code does
not need to make an assumption about the relative sizes of 'size_t' versus 'int'.

Bruno

Loading...