diff options
-rw-r--r-- | wsutil/sign_ext.h | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/wsutil/sign_ext.h b/wsutil/sign_ext.h index 4371cacde6..e680361401 100644 --- a/wsutil/sign_ext.h +++ b/wsutil/sign_ext.h @@ -35,8 +35,14 @@ ws_sign_ext32(guint32 val, int no_of_bits) if (no_of_bits == 0) return val; - if (val & (1 << (no_of_bits-1))) - val |= (-1 << no_of_bits); + /* + * Don't shift signed values left; that's not valid in C99, at + * least, if the value is negative or if the shift count is + * the number of bits in the value - 1, and we might get + * compile-time or run-time complaints about that. + */ + if (val & (1U << (no_of_bits-1))) + val |= (0xFFFFFFFFU << no_of_bits); return val; } @@ -49,8 +55,14 @@ ws_sign_ext64(guint64 val, int no_of_bits) if (no_of_bits == 0) return val; - if (val & (G_GINT64_CONSTANT(1) << (no_of_bits-1))) - val |= (G_GINT64_CONSTANT(-1) << no_of_bits); + /* + * Don't shift signed values left; that's not valid in C99, at + * least, if the value is negative or if the shift count is + * the number of bits in the value - 1, and we might get + * compile-time or run-time complaints about that. + */ + if (val & (G_GUINT64_CONSTANT(1) << (no_of_bits-1))) + val |= (G_GUINT64_CONSTANT(0xFFFFFFFFFFFFFFFF) << no_of_bits); return val; } |