core-util: Make range checks easier to read
authorTanu Kaskinen <tanuk@iki.fi>
Tue, 9 Jun 2020 04:56:17 +0000 (07:56 +0300)
committerTanu Kaskinen <tanuk@iki.fi>
Thu, 3 Dec 2020 14:41:39 +0000 (14:41 +0000)
It wasn't immediately obvious to me what these checks are supposed to
do. Explicitly checking against the min/max values should make the code
easier to understand.

Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/51>

src/pulsecore/core-util.c

index d421a75..a6473c1 100644 (file)
@@ -2189,7 +2189,7 @@ int pa_atoi(const char *s, int32_t *ret_i) {
     if (pa_atol(s, &l) < 0)
         return -1;
 
-    if ((int32_t) l != l) {
+    if (l < INT32_MIN || l > INT32_MAX) {
         errno = ERANGE;
         return -1;
     }
@@ -2233,7 +2233,7 @@ int pa_atou(const char *s, uint32_t *ret_u) {
         return -1;
     }
 
-    if ((uint32_t) l != l) {
+    if (l > UINT32_MAX) {
         errno = ERANGE;
         return -1;
     }
@@ -2277,7 +2277,7 @@ int pa_atou64(const char *s, uint64_t *ret_u) {
         return -1;
     }
 
-    if ((uint64_t) l != l) {
+    if (l > UINT64_MAX) {
         errno = ERANGE;
         return -1;
     }
@@ -2360,7 +2360,7 @@ int pa_atoi64(const char *s, int64_t *ret_l) {
 
     *ret_l = l;
 
-    if ((int64_t) l != l) {
+    if (l < INT64_MIN || l > INT64_MAX) {
         errno = ERANGE;
         return -1;
     }