Decode oid/xid >= 2^31 correctly
authorChristoph Berg <myon@debian.org>
Thu, 14 Sep 2023 12:10:38 +0000 (14:10 +0200)
committerChristoph Berg <myon@debian.org>
Thu, 14 Sep 2023 12:10:38 +0000 (14:10 +0200)
Spotted by alexandervpotapov.

Close #18.

decode.c
expected/datatypes.out
expected/datatypes_3.out

index 7542b9dc8c189ebb65435aae116dd0b9be2fcfc6..73c0dd02883772b096821eb0eb86c48edf7ebd72 100644 (file)
--- a/decode.c
+++ b/decode.c
@@ -66,6 +66,9 @@ decode_smallint(const char *buffer, unsigned int buff_size, unsigned int *out_si
 static int
 decode_int(const char *buffer, unsigned int buff_size, unsigned int *out_size);
 
+static int
+decode_uint(const char *buffer, unsigned int buff_size, unsigned int *out_size);
+
 static int
 decode_bigint(const char *buffer, unsigned int buff_size, unsigned int *out_size);
 
@@ -144,10 +147,10 @@ static ParseCallbackTableItem callback_table[] =
                "int", &decode_int
        },
        {
-               "oid", &decode_int
+               "oid", &decode_uint
        },
        {
-               "xid", &decode_int
+               "xid", &decode_uint
        },
        {
                "serial", &decode_int
@@ -702,6 +705,27 @@ decode_int(const char *buffer, unsigned int buff_size, unsigned int *out_size)
        return 0;
 }
 
+/* Decode an unsigned int type */
+static int
+decode_uint(const char *buffer, unsigned int buff_size, unsigned int *out_size)
+{
+       const char *new_buffer = (const char *) INTALIGN(buffer);
+       unsigned int delta = (unsigned int) ((uintptr_t) new_buffer - (uintptr_t) buffer);
+
+       if (buff_size < delta)
+               return -1;
+
+       buff_size -= delta;
+       buffer = new_buffer;
+
+       if (buff_size < sizeof(uint32))
+               return -2;
+
+       CopyAppendFmt("%u", *(uint32 *) buffer);
+       *out_size = sizeof(uint32) + delta;
+       return 0;
+}
+
 /* Decode a bigint type */
 static int
 decode_bigint(const char *buffer, unsigned int buff_size, unsigned int *out_size)
index ff2aae26fabd141dc63238b53168e1461c12d6af..a13c19e8ab789344befdf3fa00be2f5ae95c0243 100644 (file)
@@ -516,7 +516,7 @@ Block    0 ********************************************************
 
 <Data> -----
  Item   1 -- Length:   28  Offset: 8160 (0x1fe0)  Flags: NORMAL
-COPY: -1
+COPY: 4294967295
  Item   2 -- Length:   28  Offset: 8128 (0x1fc0)  Flags: NORMAL
 COPY: 0
  Item   3 -- Length:   28  Offset: 8096 (0x1fa0)  Flags: NORMAL
@@ -972,7 +972,7 @@ Block    0 ********************************************************
 
 <Data> -----
  Item   1 -- Length:   28  Offset: 8160 (0x1fe0)  Flags: NORMAL
-COPY: -1
+COPY: 4294967295
  Item   2 -- Length:   28  Offset: 8128 (0x1fc0)  Flags: NORMAL
 COPY: 0
  Item   3 -- Length:   28  Offset: 8096 (0x1fa0)  Flags: NORMAL
index 9620cea06b97431a98eca3afecc4963089badb41..beaedba7a732224d883aca1aaf3c290bdc79347f 100644 (file)
@@ -516,7 +516,7 @@ Block    0 ********************************************************
 
 <Data> -----
  Item   1 -- Length:   28  Offset: 8164 (0x1fe4)  Flags: NORMAL
-COPY: -1
+COPY: 4294967295
  Item   2 -- Length:   28  Offset: 8136 (0x1fc8)  Flags: NORMAL
 COPY: 0
  Item   3 -- Length:   28  Offset: 8108 (0x1fac)  Flags: NORMAL
@@ -972,7 +972,7 @@ Block    0 ********************************************************
 
 <Data> -----
  Item   1 -- Length:   28  Offset: 8164 (0x1fe4)  Flags: NORMAL
-COPY: -1
+COPY: 4294967295
  Item   2 -- Length:   28  Offset: 8136 (0x1fc8)  Flags: NORMAL
 COPY: 0
  Item   3 -- Length:   28  Offset: 8108 (0x1fac)  Flags: NORMAL