Обсуждение: Dereferenced pointers checked as NULL in btree_utils_var.c
Hi all, Coverity is pointing out $subject, with the following stuff in gbt_var_same(): GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1); GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2); GBT_VARKEY_R r1, r2; r1 = gbt_var_key_readable(t1); <= t1 dereferenced r2 = gbt_var_key_readable(t2); <= t2 dereferenced if (t1 && t2) result = ((*tinfo->f_cmp) (r1.lower, r2.lower, collation) == 0 && (*tinfo->f_cmp) (r1.upper, r2.upper, collation) == 0); else result = (t1 == NULL && t2 == NULL); <= Coverity complains here return result; As Heikki pointed me out on IM, the lack of crash report in this area, as well as similar coding style in cube/ seem to be sufficient arguments to simply remove those NULL checks instead of doing more solid checks on them. Patch is attached. Regards, -- Michael
Вложения
Michael Paquier <michael.paquier@gmail.com> writes: > Coverity is pointing out $subject, with the following stuff in gbt_var_same(): > ... > As Heikki pointed me out on IM, the lack of crash report in this area, > as well as similar coding style in cube/ seem to be sufficient > arguments to simply remove those NULL checks instead of doing more > solid checks on them. Patch is attached. The way to form a convincing argument that these checks are unnecessary would be to verify that (1) the SQL-accessible functions directly calling gbt_var_same() are all marked STRICT, and (2) the core GIST code never passes a NULL to these support functions. I'm prepared to believe that (1) and (2) are both true, but it merits checking. regards, tom lane
On Tue, Jan 20, 2015 at 11:38 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote: > Michael Paquier <michael.paquier@gmail.com> writes: >> Coverity is pointing out $subject, with the following stuff in gbt_var_same(): >> ... >> As Heikki pointed me out on IM, the lack of crash report in this area, >> as well as similar coding style in cube/ seem to be sufficient >> arguments to simply remove those NULL checks instead of doing more >> solid checks on them. Patch is attached. > > The way to form a convincing argument that these checks are unnecessary > would be to verify that (1) the SQL-accessible functions directly calling > gbt_var_same() are all marked STRICT, and (2) the core GIST code never > passes a NULL to these support functions. I'm prepared to believe that > (1) and (2) are both true, but it merits checking. Sure. gbt_var_same is called by those functions in btree_gist/: - gbt_bit_same - gbt_bytea_same - gbt_numeric_same - gbt_text_same =# select proname, proisstrict from pg_proc where proname in ('gbt_bit_same', 'gbt_bytea_same', 'gbt_numeric_same', 'gbt_text_same'); proname | proisstrict ------------------+-------------gbt_text_same | tgbt_bytea_same | tgbt_numeric_same | tgbt_bit_same | t (4 rows) For the second point, I have run regression tests with an assertion in gbt_var_same checking if t1 or t2 are NULL and tests worked. Also, looking at the code of gist, gbt_var_same is called through gistKeyIsEQ, which is used for an insertion or for a split. The point is that when doing an insertion, a call to gistgetadjusted is done and we look if one of the keys is NULL. If one of them is, code does not go through gistKeyIsEQ, so the NULL checks do not seem necessary in gbt_var_same. Btw, looking at the code of gist, I think that it would be interesting to add an assertion in gistKeyIsEQ like that: Assert(DatumGetPointer(a) != NULL && DatumGetPointer(b) != NULL); Thoughts? -- Michael
On 01/21/2015 07:14 AM, Michael Paquier wrote: > Also, > looking at the code of gist, gbt_var_same is called through > gistKeyIsEQ, which is used for an insertion or for a split. The point > is that when doing an insertion, a call to gistgetadjusted is done and > we look if one of the keys is NULL. If one of them is, code does not > go through gistKeyIsEQ, so the NULL checks do not seem necessary in > gbt_var_same. I think you are confusing NULL pointers with an SQL NULL. gistgetadjusted checks that it's not an SQL NULL (!oldisnull[i]), but it does not check if it's a NULL pointer (DatumGetPointer(oldentries[i].key) != NULL). The case we're worried about is that the value is not an SQL NULL, i.e. isnull flag is false, but the Datum is a NULL pointer. Nevertheless, looking at the code, I don't that a NULL pointer can ever be passed to the same-method, for any of the built-in or contrib opclasses, but it's very confusing because some functions check for that. My proof goes like this: 1. The key value passed as argument must've been originally formed by the compress, union, or picksplit methods. 1.1. Some compress methods do nothing, and just return the passed-in key, which comes from the table and cannot be a NULL pointer (the compress method is never called for SQL NULLs). Other compress methods don't check for a NULL pointer, and would crash if there was one. gist_poly_compress() and gist_circle_compress() do check for a NULL, but they only return a NULL key if the input key is NULL, which cannot happen because the input comes from a table. So I believe the NULL-checks in those functions are dead code, and none of the compress methods ever return a NULL key. 1.2. None of the union methods return a NULL pointer (nor expect one as input). 1.3. None of the picksplit methods return a NULL pointer. They all return one of the original values, or one formed with the union method. The picksplit method can return a NULL pointer in the spl_ldatum or spl_rdatum field, in the degenerate case that it puts all keys on the same halve. However, the caller, gistUserPickSplit checks for that and immediately overwrites spl_ldatum and spl_rdatum with sane values in that case. I don't understand what this sentence in the documentation on the compress method means: > Depending on your needs, you could also need to care about > compressing NULL values in there, storing for example (Datum) 0 like > gist_circle_compress does. The compress method is never called for NULLs, so the above is nonsense. I think it should be removed, as well as the checks in gist_circle_compress and gist_poly_compress. According to git history, the check in gist_circle_compress been there ever since the module was imported into contrib/rtree_gist, in 2001. The documentation was added later: commit a0a3883dd977d6618899ccd14258a0696912a9d2 Author: Tom Lane <tgl@sss.pgh.pa.us> Date: Fri Jun 12 19:48:53 2009 +0000 Improve documentation about GiST opclass support functions. Dimitri Fontaine I'm guessing that Tom added that sentence (it was not in the patch that Dimitri submitted originally) just because there was that check in the existing function, without realizing that the check was in fact dead code. - Heikki
Heikki Linnakangas <hlinnakangas@vmware.com> writes: > I think you are confusing NULL pointers with an SQL NULL. > gistgetadjusted checks that it's not an SQL NULL (!oldisnull[i]), but it > does not check if it's a NULL pointer > (DatumGetPointer(oldentries[i].key) != NULL). The case we're worried > about is that the value is not an SQL NULL, i.e. isnull flag is false, > but the Datum is a NULL pointer. Actually both of these deserve to be worried about; though it's fairly clear from looking at the core GIST code that it avoids calling gistKeyIsEQ on SQL NULLs. > Nevertheless, looking at the code, I don't that a NULL pointer can ever > be passed to the same-method, for any of the built-in or contrib > opclasses, but it's very confusing because some functions check for > that. My proof goes like this: > 1. The key value passed as argument must've been originally formed by > the compress, union, or picksplit methods. > 1.1. Some compress methods do nothing, and just return the passed-in > key, which comes from the table and cannot be a NULL pointer (the > compress method is never called for SQL NULLs). Other compress methods > don't check for a NULL pointer, and would crash if there was one. > gist_poly_compress() and gist_circle_compress() do check for a NULL, but > they only return a NULL key if the input key is NULL, which cannot > happen because the input comes from a table. So I believe the > NULL-checks in those functions are dead code, and none of the compress > methods ever return a NULL key. > 1.2. None of the union methods return a NULL pointer (nor expect one as > input). > 1.3. None of the picksplit methods return a NULL pointer. They all > return one of the original values, or one formed with the union method. > The picksplit method can return a NULL pointer in the spl_ldatum or > spl_rdatum field, in the degenerate case that it puts all keys on the > same halve. However, the caller, gistUserPickSplit checks for that and > immediately overwrites spl_ldatum and spl_rdatum with sane values in > that case. Sounds good to me. > I don't understand what this sentence in the documentation on the > compress method means: >> Depending on your needs, you could also need to care about >> compressing NULL values in there, storing for example (Datum) 0 like >> gist_circle_compress does. I believe you're right that I added this because there were checks for null pointers in some but not all of the opclass support functions. It looked to me like some opclasses might be intending to pass around null pointers as valid (not-SQL-NULL) values. I think your analysis above eliminates that idea though. It's a sufficiently weird concept that I don't feel a need to document or support it. So I'm fine with taking out both this documentation text and the dead null-pointer checks; but let's do that all in one patch not piecemeal. In any case, this is just cosmetic cleanup; there's no actual hazard here. regards, tom lane
On Wed, Jan 28, 2015 at 3:15 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote: > So I'm fine with taking out both this documentation text and the dead > null-pointer checks; but let's do that all in one patch not piecemeal. > In any case, this is just cosmetic cleanup; there's no actual hazard > here. Attached is a patch with all those things done. I added as well an assertion in gistKeyIsEQ checking if the input datums are NULL. I believe that this is still useful for developers, feel free to rip it out from the patch if you think otherwise. Regards, -- Michael
Вложения
On 01/28/2015 02:47 AM, Michael Paquier wrote: > On Wed, Jan 28, 2015 at 3:15 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote: >> So I'm fine with taking out both this documentation text and the dead >> null-pointer checks; but let's do that all in one patch not piecemeal. >> In any case, this is just cosmetic cleanup; there's no actual hazard >> here. > Attached is a patch with all those things done. Thanks, applied. > I added as well an assertion in gistKeyIsEQ checking if the input > datums are NULL. I believe that this is still useful for developers, > feel free to rip it out from the patch if you think otherwise. I ripped it out because I think was wrong. It assumed that the input Datums are pass-by-reference, which is not a given. It looks that's true for all the current opclasses, so I wouldn't be surprised if there are hidden assumptions on that elsewhere in the code, but it was wrong nevertheless. - Heikki