qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kgiu...@apache.org
Subject qpid-dispatch git commit: DISPATCH-1205: accept valid integer sign coversions when parsing
Date Mon, 03 Dec 2018 21:14:07 GMT
Repository: qpid-dispatch
Updated Branches:
  refs/heads/master 663a997c3 -> 93c89a758


DISPATCH-1205: accept valid integer sign coversions when parsing

This closes #422


Project: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/commit/93c89a75
Tree: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/tree/93c89a75
Diff: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/diff/93c89a75

Branch: refs/heads/master
Commit: 93c89a75825a57a5749d262f4b5be26301f70c82
Parents: 663a997
Author: Kenneth Giusti <kgiusti@apache.org>
Authored: Mon Dec 3 13:57:00 2018 -0500
Committer: Kenneth Giusti <kgiusti@apache.org>
Committed: Mon Dec 3 16:13:07 2018 -0500

----------------------------------------------------------------------
 src/parse.c          | 55 ++++++++++++++++++++++++++++++++++++++++--
 tests/CMakeLists.txt |  2 +-
 tests/parse_test.c   | 61 ++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 112 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/93c89a75/src/parse.c
----------------------------------------------------------------------
diff --git a/src/parse.c b/src/parse.c
index c00c9fd..3ab33dc 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -364,7 +364,7 @@ uint32_t qd_parse_as_uint(qd_parsed_field_t *field)
     uint32_t result = 0;
     uint64_t tmp = qd_parse_as_ulong(field);
     if (qd_parse_ok(field)) {
-        if (tmp <= UINT_MAX) {
+        if (tmp <= UINT32_MAX) {
             result = tmp;
         } else {
             field->parse_error = "Integer value too large to parse as uint";
@@ -430,6 +430,26 @@ uint64_t qd_parse_as_ulong(qd_parsed_field_t *field)
         }
         break;
 
+    case QD_AMQP_BYTE:
+    case QD_AMQP_SHORT:
+    case QD_AMQP_INT:
+    case QD_AMQP_SMALLINT:
+    case QD_AMQP_LONG:
+    case QD_AMQP_SMALLLONG:
+    {
+        // if a signed integer is positive, accept it
+        int64_t ltmp = qd_parse_as_long(field);
+        if (qd_parse_ok(field)) {
+            if (ltmp >= 0) {
+                result = (uint64_t)ltmp;
+            } else {
+                field->parse_error = "Unable to parse negative integer as unsigned";
+            }
+        }
+    }
+    break;
+
+
     default:
         field->parse_error = "Unable to parse as an unsigned integer";
         // catch any missing types during development
@@ -445,7 +465,7 @@ int32_t qd_parse_as_int(qd_parsed_field_t *field)
     int32_t result = 0;
     int64_t tmp = qd_parse_as_long(field);
     if (qd_parse_ok(field)) {
-        if (INT_MIN <= tmp && tmp <= INT_MAX) {
+        if (INT32_MIN <= tmp && tmp <= INT32_MAX) {
             result = tmp;
         } else {
             field->parse_error = "Integer value too large to parse as int";
@@ -522,6 +542,37 @@ int64_t qd_parse_as_long(qd_parsed_field_t *field)
         }
         break;
 
+    case QD_AMQP_UBYTE:
+    case QD_AMQP_SMALLUINT:
+    case QD_AMQP_SMALLULONG:
+    case QD_AMQP_USHORT:
+    case QD_AMQP_UINT:
+    case QD_AMQP_ULONG:
+    {
+        // if an unsigned integer "fits" accept it
+        uint64_t utmp = qd_parse_as_ulong(field);
+        if (qd_parse_ok(field)) {
+            uint64_t max = INT8_MAX;
+            switch (field->tag) {
+            case QD_AMQP_USHORT:
+                max = INT16_MAX;
+                break;
+            case QD_AMQP_UINT:
+                max = INT32_MAX;
+                break;
+            case QD_AMQP_ULONG:
+                max = INT64_MAX;
+                break;
+            }
+            if (utmp <= max) {
+                result = (int64_t)utmp;
+            } else {
+                field->parse_error = "Unable to parse unsigned integer as a signed integer";
+            }
+        }
+    }
+    break;
+
     default:
         field->parse_error = "Unable to parse as a signed integer";
         // catch any missing types during development

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/93c89a75/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 1075c76..51dc438 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -33,7 +33,7 @@ set(unit_test_SOURCES
     failoverlist_test.c
     timer_test.c
     core_timer_test.c
-    parse_tree_tests
+    parse_tree_tests.c
     proton_utils_tests.c
     )
 if (USE_MEMORY_POOL)

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/93c89a75/tests/parse_test.c
----------------------------------------------------------------------
diff --git a/tests/parse_test.c b/tests/parse_test.c
index 15b99bb..ac678f5 100644
--- a/tests/parse_test.c
+++ b/tests/parse_test.c
@@ -145,10 +145,45 @@ static char *test_integer_conversion(void *context)
         {"\x81\xFF\xFF\xFF\xFF\x80\x00\x00\x00", 9, QD_AMQP_INT,  false, INT32_MIN, 0},
         {"\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9, QD_AMQP_INT,  false, -1,        0},
 
-        // and cannot convert 64 bit values that are outside the 32bit range as int32
+        // signed/unsigned conversions
+        {"\x70\x7F\xFF\xFF\xFF",                 5, QD_AMQP_INT,  false, INT32_MAX, 0},
+        {"\x71\x7F\xFF\xFF\xFF",                 5, QD_AMQP_UINT, false, 0,         INT32_MAX},
+        {"\x80\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9, QD_AMQP_LONG, false, INT64_MAX, 0},
+        {"\x81\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9, QD_AMQP_ULONG, false,0,         INT64_MAX},
+        {"\x50\x7F",                             2, QD_AMQP_INT,  false, INT8_MAX,  0},
+        {"\x60\x7F\xFF",                         3, QD_AMQP_INT,  false, INT16_MAX,  0},
+        {"\x53\x7F",                             2, QD_AMQP_INT,  false, INT8_MAX,  0},
+        {"\x55\x7F",                             2, QD_AMQP_UINT, false, 0,         INT8_MAX},
+        {"\x51\x7F",                             2, QD_AMQP_UINT, false, 0,         INT8_MAX},
+        {"\x61\x7F\xFF",                         3, QD_AMQP_UINT, false, 0,         INT16_MAX},
+
+        // strings
+        {"\xa1\x02 1",                           4, QD_AMQP_UINT, false, 0,         1},
+        {"\xa1\x02-1",                           4, QD_AMQP_INT,  false, -1,        0},
+
+        {"\xa1\x14" "18446744073709551615",     22, QD_AMQP_ULONG,false, 0,         UINT64_MAX},
+        {"\xa1\x14" "-9223372036854775808",     22, QD_AMQP_LONG, false, INT64_MIN, 0},
+        {"\xa1\x13" "9223372036854775807",      21, QD_AMQP_LONG, false, INT64_MAX, 0},
+        {"\xa3\x13" "9223372036854775807",      21, QD_AMQP_LONG, false, INT64_MAX, 0},
+
+        // cannot convert 64 bit values that are outside the 32bit range as int32
         {"\x80\x00\x00\x00\x01\x00\x00\x00\x00", 9, QD_AMQP_UINT, true,  0, 0},
         {"\x81\x00\x00\x00\x00\x80\x00\x00\x00", 9, QD_AMQP_INT,  true,  0, 0},
         {"\x81\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF", 9, QD_AMQP_INT,  true,  0, 0},
+
+        // bad signed/unsigned conversions
+        {"\x80\x80\x00\x00\x00\x00\x00\x00\x00", 9, QD_AMQP_LONG,  true, 0, 0},
+        {"\x81\x80\x00\x00\x00\x00\x00\x00\x00", 9, QD_AMQP_ULONG, true, 0, 0},
+        {"\x70\x80\x00\x00\x00",                 5, QD_AMQP_LONG,  true, 0, 0},
+        {"\x71\x80\x00\x00\x00",                 5, QD_AMQP_ULONG, true, 0, 0},
+        {"\x55\x80",                             2, QD_AMQP_UINT,  true, 0, 0},
+        {"\x51\x80",                             2, QD_AMQP_UINT,  true, 0, 0},
+        {"\x54\x80",                             2, QD_AMQP_UINT,  true, 0, 0},
+        {"\x61\x80\x00",                         3, QD_AMQP_UINT,  true, 0, 0},
+        {"\x53\x80",                             2, QD_AMQP_INT,   true, 0, 0},
+        {"\x52\x80",                             2, QD_AMQP_INT,   true, 0, 0},
+        {"\x50\x80",                             2, QD_AMQP_LONG,  true, 0, 0},
+        {"\x60\x80",                             2, QD_AMQP_LONG,  true, 0, 0},
         {NULL},
     };
 
@@ -165,12 +200,32 @@ static char *test_integer_conversion(void *context)
         }
 
         bool equal = false;
-        if (fs_vectors[i].parse_as == QD_AMQP_UINT) {
+        switch (fs_vectors[i].parse_as) {
+        case QD_AMQP_UINT:
+        {
             uint32_t tmp = qd_parse_as_uint(field);
             equal = (tmp == fs_vectors[i].expected_uint);
-        } else {
+            break;
+        }
+        case QD_AMQP_ULONG:
+        {
+            uint64_t tmp = qd_parse_as_ulong(field);
+            equal = (tmp == fs_vectors[i].expected_uint);
+            break;
+        }
+        case QD_AMQP_INT:
+        {
+
             int32_t tmp = qd_parse_as_int(field);
             equal = (tmp == fs_vectors[i].expected_int);
+            break;
+        }
+        case QD_AMQP_LONG:
+        {
+            int64_t tmp = qd_parse_as_long(field);
+            equal = (tmp == fs_vectors[i].expected_int);
+            break;
+        }
         }
 
         if (!qd_parse_ok(field)) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message