Thread: invalid memory alloc request size with extension url_decode
hi all, using the extension url_encode (not on pgxn) does not work in postgresql 9.3 (and 9.4). (i assume it works in earlier versions, but i have not tested that.) the testcase is the following. create table t (id serial primary key, t1 text not null, t2 text); insert into t (t1) values ('test'); update t set t2 = url_encode(t1); it fails with Error: invalid memory alloc request size 1452021511 for my untrained eye, the code [2] looks reasonable. it does use the following idiom though. static text * encode(text *in_text, ...) { ... real_len = 0; len = VARSIZE(in_text) - VARHDRSZ; result = (text *) palloc(3 * len + VARHDRSZ); write_ptr = VARDATA(result); ... SET_VARSIZE(result, real_len + VARHDRSZ); return result; } is that safe for "text" data type? how should it be replaced if not? [1] https://github.com/okbob/url_encode [2] https://github.com/okbob/url_encode/blob/master/src/url_encode.c thank you in advance, tobias florek
hi, > using the extension url_encode (not on pgxn) does not work in postgresql > 9.3 (and 9.4). (i assume it works in earlier versions, but i have not > tested that.) i in fact assume it does _not_ work in earlier version. sorry for any confusion. tobias florek
Tobias Florek <postgres@ibotty.net> writes: > for my untrained eye, the code [2] looks reasonable. it does use the > following idiom though. > len = VARSIZE(in_text) - VARHDRSZ; This would be okay except that the in_text value was extracted with PG_GETARG_TEXT_PP rather than PG_GETARG_TEXT_P. That allows the value to be a short-header Datum, which this code is not prepared for. You could either change the wrapper functions to use PG_GETARG_TEXT_P, or fix the subroutines to use VARDATA_ANY and VARSIZE_ANY_EXHDR when inspecting their text* arguments. regards, tom lane