summaryrefslogtreecommitdiff
path: root/wsutil/sign_ext.h
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2015-04-30 13:39:39 -0700
committerGuy Harris <guy@alum.mit.edu>2015-04-30 20:40:17 +0000
commitd2b02eaf591145f40eaa65d6b50908e47d7c4484 (patch)
tree43b7a0df4e0b7a7212bc084cd401aac1c26cfce7 /wsutil/sign_ext.h
parentfac11078c3b5ace9b84c1058fb44714339f91190 (diff)
downloadwireshark-d2b02eaf591145f40eaa65d6b50908e47d7c4484.tar.gz
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. Also, make bit masks unsigned; to quote a run-time error reported in https://www.wireshark.org/lists/wireshark-dev/201504/msg00084.html "left shift of 1 by 31 places cannot be represented in type 'int'", so use type "unsigned int" instead, by shifting 1U rather than 1 left. Change-Id: I62220808058cb93f83329c1916b888a2067d524c Reviewed-on: https://code.wireshark.org/review/8254 Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wsutil/sign_ext.h')
-rw-r--r--wsutil/sign_ext.h20
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;
}