Subject: Re: eclg -C ORACLE breaks data

From: Michael Paquier <[email-masked]>
Date: Mon, 17 Apr 2023 17:00:59 +0900

On Mon, Apr 10, 2023 at 05:35:00PM +0900, Kyotaro Horiguchi wrote:
> This results in overwriting str[-1], the last byte of the preceding
> numeric in this case, with 0x00, representing the digit '0'. When
> callers of ecpg_get_data() explicitly pass zero as varcharsize, they
> provide storage that precisely fitting the data.

Good find, that's clearly wrong.  The test case is interesting.  On
HEAD, the processing of the second field eats up the data of the first
field.

> However, it remains
> uncertain if this assumption is valid when ecpg_store_result() passes
> var->varcharsize which is also zero. Consequently, the current fix
> presumes exact-fit storage when varcharsize is zero.

Based on what I can read in sqlda.c (ecpg_set_compat_sqlda() and
ecpg_set_native_sqlda()), the data length calculated adds an extra
byte to the data length when storing the data references in sqldata.
execute.c and ecpg_store_result() is actually much trickier than that
(see particularly the part where the code does an "allocate memory for
NULL pointers", where varcharsize could also be 0), still I agree that
this assumption should be OK.  The code is as it is for many years,
with its logic to do an estimation of allocation first, and then read
the data at once in the whole area allocated..

This thinko has been introduced by 3b7ab43, so this needs to go down
to v11.  I'll see to that.
--
Michael

---

From: Kyotaro Horiguchi <[email-masked]>
Date: Mon, 17 Apr 2023 17:47:41 +0900 (JST)

(sorry for the wrong subject..)

At Mon, 17 Apr 2023 17:00:59 +0900, Michael Paquier <[email-masked]> wrote in 
> to v11.  I'll see to that.

Thanks for picking this up.

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center

---

From: Michael Paquier <[email-masked]>
Date: Tue, 18 Apr 2023 11:35:16 +0900

> to v11.  I'll see to that.

So done.  mylodon is feeling unhappy about that, because this has a
C99 declaration:
https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=mylodon&dt=2023-04-18%2002%3A22%3A04
char_array.pgc:73:8: error: variable declaration in for loop is a C99-specific feature [-Werror,-Wc99-extensions]
  for (int i = 0 ; i < sqlda->sqld ; i++)

I'll go fix that in a minute, across all the branches for
consistency.
--
Michael

---

From: Kyotaro Horiguchi <[email-masked]>
Date: Thu, 20 Apr 2023 12:56:32 +0900 (JST)

At Tue, 18 Apr 2023 11:35:16 +0900, Michael Paquier <[email-masked]> wrote in 
> So done.  mylodon is feeling unhappy about that, because this has a

Thanks!

> I'll go fix that in a minute, across all the branches for

Oh, I didn't realize there were differences in the
configurations. Good to know.

regards.

---

From: Michael Paquier <[email-masked]>
Date: Thu, 20 Apr 2023 13:00:52 +0900

> configurations. Good to know.

C99 declarations are OK in v12~, so with v11 going out of sight in
approximately 6 month, it won't matter soon ;)
--
Michael