[Bug Fix]ECPG: cancellation of significant digits on ECPG
Higuchi, Daisuke <higuchi.daisuke@jp.fujitsu.com>
From: "Higuchi, Daisuke" <higuchi.daisuke@jp.fujitsu.com>
To: "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>
Date: 2018-05-17T04:09:38Z
Lists: pgsql-hackers
Attachments
- 001_ecpg_numeric_bugfix_v1.patch (application/octet-stream) patch v1
- numeric_sample_test.pgc (application/octet-stream)
- 002_ecpg_numeric_test_v1.patch (application/octet-stream) patch v1
Hi,
Currently our customer uses PostgreSQL 9.6 and hits ECPG's bug during using numeric data type by SQLDA.
I confirmed that this problem is occurred on master and 9.6 latest branch.
PROBLEM
---------------------------------------
When the integer part of numeric data type is "0", cancellation of significant digits is occurred.
For example, I expect "0.12345", but I got "0.12340". When I expect "0.01234", I got "0.01200"
I attached the sample application code to reproduce this problem.
CAUSE
---------------------------------------
When copy the data of numeric data, the size is wrong.
"src/interfaces/ecpg/ecpglib/sqlda.c" has problem.
ecpg_set_native_sqlda(int lineno, struct sqlda_struct ** _sqlda, const PGresult *res, int row, enum COMPAT_MODE compat)
{
...
if (num->ndigits)
{
ecpg_sqlda_align_add_size(next_offset, sizeof(int), num->ndigits + 1, &offset, &next_offset);
memcpy((char *) sqlda + offset, num->buf, num->ndigits + 1);
((numeric *) sqlda->sqlvar[i].sqldata)->buf = (NumericDigit *) sqlda + offset;
((numeric *) sqlda->sqlvar[i].sqldata)->digits = (NumericDigit *) sqlda + offset + (num->digits - num->buf);
}
...
When numeric data is "0.12345", num->buf has "0 0 1 2 3 4 5" and num->digits has "1 2 3 4 5".
num->ndigits has the number of digits which is or later "1", it means 5.
In this code, currently copy "num->ndigits + 1" as size of numeric data.
As a result, (char *) sqlda + offset has "0 0 1 2 3 4", not "0 0 1 2 3 4 5".
So, "num->digits - num->buf + num->ndigits" should be copied.
FIX
---------------------------------------
Above source code should be fixed and other similar bugs are fixed too.
I attached patches for bug fix and regression test for master branch.
I hope this bug fix will be backport to other versions.
Regards,
Daisuke Higuchi
Commits
-
Second try at fixing numeric data passed through an ECPG SQLDA.
- 06c723447b6b 12.0 landed
- f9e25ba14077 9.6.12 landed
- 68f30638a050 9.5.16 landed
- 41609776f2c2 9.4.21 landed
- 4618fdd67412 11.2 landed
- 2e8ed4659972 10.7 landed
-
Fix incorrect results for numeric data passed through an ECPG SQLDA.
- ecfd5579561c 12.0 landed
- e1f25901268c 9.6.12 landed
- be38945c6e66 9.5.16 landed
- 9e5e3861c782 9.4.21 landed
- 68393f3fd6b9 11.2 landed
- 32060f6780bc 10.7 landed