ecpg: Fix return code for overflow in numeric conversion
authorDaniel Gustafsson <dgustafsson@postgresql.org>
Mon, 25 Mar 2024 13:18:36 +0000 (14:18 +0100)
committerDaniel Gustafsson <dgustafsson@postgresql.org>
Mon, 25 Mar 2024 13:18:36 +0000 (14:18 +0100)
The decimal conversion functions dectoint and dectolong are documented
to return ECPG_INFORMIX_NUM_OVERFLOW in case of overflows, but always
returned -1 on all errors due to incorrectly checking the returnvalue
from the PGTYPES* functions.

Author: Aidar Imamov <a.imamov@postgrespro.ru>
Discussion: https://postgr.es/m/54d2b53327516d9454daa5fb2f893bdc@postgrespro.ru

src/interfaces/ecpg/compatlib/informix.c
src/interfaces/ecpg/test/expected/compat_informix-dec_test.stdout

index 80d40aa3e09b2c8526f6fdf3148791589ae363b6..8ea89e640a0838945560b76a20af885900eae70a 100644 (file)
@@ -435,6 +435,7 @@ dectoint(decimal *np, int *ip)
 {
    int         ret;
    numeric    *nres = PGTYPESnumeric_new();
+   int         errnum;
 
    if (nres == NULL)
        return ECPG_INFORMIX_OUT_OF_MEMORY;
@@ -445,10 +446,12 @@ dectoint(decimal *np, int *ip)
        return ECPG_INFORMIX_OUT_OF_MEMORY;
    }
 
+   errno = 0;
    ret = PGTYPESnumeric_to_int(nres, ip);
+   errnum = errno;
    PGTYPESnumeric_free(nres);
 
-   if (ret == PGTYPES_NUM_OVERFLOW)
+   if (ret == -1 && errnum == PGTYPES_NUM_OVERFLOW)
        ret = ECPG_INFORMIX_NUM_OVERFLOW;
 
    return ret;
@@ -459,6 +462,7 @@ dectolong(decimal *np, long *lngp)
 {
    int         ret;
    numeric    *nres = PGTYPESnumeric_new();
+   int         errnum;
 
    if (nres == NULL)
        return ECPG_INFORMIX_OUT_OF_MEMORY;
@@ -469,10 +473,12 @@ dectolong(decimal *np, long *lngp)
        return ECPG_INFORMIX_OUT_OF_MEMORY;
    }
 
+   errno = 0;
    ret = PGTYPESnumeric_to_long(nres, lngp);
+   errnum = errno;
    PGTYPESnumeric_free(nres);
 
-   if (ret == PGTYPES_NUM_OVERFLOW)
+   if (ret == -1 && errnum == PGTYPES_NUM_OVERFLOW)
        ret = ECPG_INFORMIX_NUM_OVERFLOW;
 
    return ret;
index 1f8675b3f3c3be920a0abd9c46332c4f9e7ef95f..71a5afa4a7ad5a562b7d86cb958bff51dde28644 100644 (file)
@@ -3,8 +3,8 @@
 (no errno set) - dec[0,3]: r: -1, *
 (no errno set) - dec[0,4]: r: -1, *
 dec[0,5]: r: 0, 0.00
-(errno == PGTYPES_NUM_OVERFLOW) - dec[0,6]: 0 (r: -1)
-(errno == PGTYPES_NUM_OVERFLOW) - dec[0,8]: 0 (r: -1)
+(errno == PGTYPES_NUM_OVERFLOW) - dec[0,6]: 0 (r: -1200)
+(errno == PGTYPES_NUM_OVERFLOW) - dec[0,8]: 0 (r: -1200)
 (errno == PGTYPES_NUM_OVERFLOW) - dec[0,10]: 0 (r: -1)
 
 dec[1,1]: r: 0, -2
@@ -45,8 +45,8 @@ dec[4,2]: r: 0, 592490000000000000000000
 dec[4,3]: r: 0, 592490000000000000000000.0
 dec[4,4]: r: 0, 592490000000000000000000.00
 dec[4,5]: r: 0, 0.00
-(errno == PGTYPES_NUM_OVERFLOW) - dec[4,6]: 0 (r: -1)
-(errno == PGTYPES_NUM_OVERFLOW) - dec[4,8]: 0 (r: -1)
+(errno == PGTYPES_NUM_OVERFLOW) - dec[4,6]: 0 (r: -1200)
+(errno == PGTYPES_NUM_OVERFLOW) - dec[4,8]: 0 (r: -1200)
 dec[4,10]: 5.9249e+23 (r: 0)
 
 dec[5,1]: r: 0, -328400
@@ -141,8 +141,8 @@ dec[13,2]: r: 0, 1234567890123456789012345679
 dec[13,3]: r: 0, 1234567890123456789012345678.9
 dec[13,4]: r: 0, 1234567890123456789012345678.91
 dec[13,5]: r: 0, 0.00
-(errno == PGTYPES_NUM_OVERFLOW) - dec[13,6]: 0 (r: -1)
-(errno == PGTYPES_NUM_OVERFLOW) - dec[13,8]: 0 (r: -1)
+(errno == PGTYPES_NUM_OVERFLOW) - dec[13,6]: 0 (r: -1200)
+(errno == PGTYPES_NUM_OVERFLOW) - dec[13,8]: 0 (r: -1200)
 dec[13,10]: 1.23457e+27 (r: 0)
 
 (errno == PGTYPES_NUM_OVERFLOW) - dec[14,0]: r: -1200