Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b196b7f

Browse files
committed
Get rid of dependency on strtoull() --- Marko Kreen.
Some additional minor editorializing by Tom.
1 parent 7121cc9 commit b196b7f

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

contrib/txid/expected/txid.out

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ select '12:13:'::txid_snapshot;
88
(1 row)
99

1010
select '12:13:1,2'::txid_snapshot;
11-
ERROR: illegal txid_snapshot input format
11+
ERROR: invalid input for txid_snapshot: "12:13:1,2"
1212
-- errors
1313
select '31:12:'::txid_snapshot;
14-
ERROR: illegal txid_snapshot input format
14+
ERROR: invalid input for txid_snapshot: "31:12:"
1515
select '0:1:'::txid_snapshot;
16-
ERROR: illegal txid_snapshot input format
16+
ERROR: invalid input for txid_snapshot: "0:1:"
1717
select '12:13:0'::txid_snapshot;
18-
ERROR: illegal txid_snapshot input format
18+
ERROR: invalid input for txid_snapshot: "12:13:0"
1919
select '12:16:14,13'::txid_snapshot;
20-
ERROR: illegal txid_snapshot input format
20+
ERROR: invalid input for txid_snapshot: "12:16:14,13"
2121
select '12:16:14,14'::txid_snapshot;
22-
ERROR: illegal txid_snapshot input format
22+
ERROR: invalid input for txid_snapshot: "12:16:14,14"
2323
create table snapshot_test (
2424
nr integer,
2525
snap txid_snapshot
@@ -210,3 +210,12 @@ select txid_visible_in_snapshot('1000100010001015', '1000100010001000:1000100010
210210
t
211211
(1 row)
212212

213+
-- test 64bit overflow
214+
SELECT txid_snapshot '1:9223372036854775807:3';
215+
txid_snapshot
216+
-------------------------
217+
1:9223372036854775807:3
218+
(1 row)
219+
220+
SELECT txid_snapshot '1:9223372036854775808:3';
221+
ERROR: invalid input for txid_snapshot: "1:9223372036854775808:3"

contrib/txid/sql/txid.sql

+3
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ select txid_snapshot '1000100010001000:1000100010001100:1000100010001012,1000100
5656
select txid_visible_in_snapshot('1000100010001012', '1000100010001000:1000100010001100:1000100010001012,1000100010001013');
5757
select txid_visible_in_snapshot('1000100010001015', '1000100010001000:1000100010001100:1000100010001012,1000100010001013');
5858

59+
-- test 64bit overflow
60+
SELECT txid_snapshot '1:9223372036854775807:3';
61+
SELECT txid_snapshot '1:9223372036854775808:3';

contrib/txid/txid.c

+41-7
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,39 @@ buf_finalize(StringInfo buf)
225225
return snap;
226226
}
227227

228+
/*
229+
* simple number parser.
230+
*
231+
* We return 0 on error, which is invalid value for txid.
232+
*/
233+
static txid
234+
str2txid(const char *s, const char **endp)
235+
{
236+
txid val = 0;
237+
238+
for (; *s; s++)
239+
{
240+
txid last = val;
241+
242+
if (*s < '0' || *s > '9')
243+
break;
244+
245+
val = val * 10 + (*s - '0');
246+
247+
/*
248+
* check for overflow
249+
*/
250+
if (val > MAX_TXID || (val / 10) != last)
251+
{
252+
val = 0;
253+
break;
254+
}
255+
}
256+
if (endp)
257+
*endp = s;
258+
return val;
259+
}
260+
228261
/*
229262
* parse snapshot from cstring
230263
*/
@@ -234,21 +267,22 @@ parse_snapshot(const char *str)
234267
txid xmin;
235268
txid xmax;
236269
txid last_val = 0, val;
237-
char *endp;
270+
const char *str_start = str;
271+
const char *endp;
238272
StringInfo buf;
239273

240-
xmin = (txid) strtoull(str, &endp, 0);
274+
xmin = str2txid(str, &endp);
241275
if (*endp != ':')
242276
goto bad_format;
243277
str = endp + 1;
244278

245-
xmax = (txid) strtoull(str, &endp, 0);
279+
xmax = str2txid(str, &endp);
246280
if (*endp != ':')
247281
goto bad_format;
248282
str = endp + 1;
249283

250284
/* it should look sane */
251-
if (xmin > xmax || xmin == 0 || xmax > MAX_TXID)
285+
if (xmin == 0 || xmax == 0 || xmin > xmax)
252286
goto bad_format;
253287

254288
/* allocate buffer */
@@ -258,11 +292,11 @@ parse_snapshot(const char *str)
258292
while (*str != '\0')
259293
{
260294
/* read next value */
261-
val = (txid) strtoull(str, &endp, 0);
295+
val = str2txid(str, &endp);
262296
str = endp;
263297

264298
/* require the input to be in order */
265-
if (val < xmin || val <= last_val || val >= xmax)
299+
if (val < xmin || val >= xmax || val <= last_val)
266300
goto bad_format;
267301

268302
buf_add_txid(buf, val);
@@ -277,7 +311,7 @@ parse_snapshot(const char *str)
277311
return buf_finalize(buf);
278312

279313
bad_format:
280-
elog(ERROR, "illegal txid_snapshot input format");
314+
elog(ERROR, "invalid input for txid_snapshot: \"%s\"", str_start);
281315
return NULL;
282316
}
283317

0 commit comments

Comments
 (0)