mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-04 17:47:20 +02:00
Fix cidin() to handle values above 2^31 platform-independently.
CommandId is declared as uint32, and values up to 4G are indeed legal. cidout() handles them properly by treating the value as unsigned int. But cidin() was just using atoi(), which has platform-dependent behavior for values outside the range of signed int, as reported by Bart Lengkeek in bug #14379. Use strtoul() instead, as xidin() does. In passing, make some purely cosmetic changes to make xidin/xidout look more like cidin/cidout; the former didn't have a monopoly on best practice IMO. Neither xidin nor cidin make any attempt to throw error for invalid input. I didn't change that here, and am not sure it's worth worrying about since neither is really a user-facing type. The point is just to ensure that indubitably-valid inputs work as expected. It's been like this for a long time, so back-patch to all supported branches. Report: <20161018152550.1413.6439@wrigleys.postgresql.org>
This commit is contained in:
parent
75c9d9a674
commit
219d476d8a
@ -41,13 +41,10 @@ Datum
|
|||||||
xidout(PG_FUNCTION_ARGS)
|
xidout(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
TransactionId transactionId = PG_GETARG_TRANSACTIONID(0);
|
TransactionId transactionId = PG_GETARG_TRANSACTIONID(0);
|
||||||
|
char *result = (char *) palloc(16);
|
||||||
|
|
||||||
/* maximum 32 bit unsigned integer representation takes 10 chars */
|
snprintf(result, 16, "%lu", (unsigned long) transactionId);
|
||||||
char *str = palloc(11);
|
PG_RETURN_CSTRING(result);
|
||||||
|
|
||||||
snprintf(str, 11, "%lu", (unsigned long) transactionId);
|
|
||||||
|
|
||||||
PG_RETURN_CSTRING(str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -148,12 +145,9 @@ xidComparator(const void *arg1, const void *arg2)
|
|||||||
Datum
|
Datum
|
||||||
cidin(PG_FUNCTION_ARGS)
|
cidin(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
char *s = PG_GETARG_CSTRING(0);
|
char *str = PG_GETARG_CSTRING(0);
|
||||||
CommandId c;
|
|
||||||
|
|
||||||
c = atoi(s);
|
PG_RETURN_COMMANDID((CommandId) strtoul(str, NULL, 0));
|
||||||
|
|
||||||
PG_RETURN_COMMANDID(c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -165,7 +159,7 @@ cidout(PG_FUNCTION_ARGS)
|
|||||||
CommandId c = PG_GETARG_COMMANDID(0);
|
CommandId c = PG_GETARG_COMMANDID(0);
|
||||||
char *result = (char *) palloc(16);
|
char *result = (char *) palloc(16);
|
||||||
|
|
||||||
snprintf(result, 16, "%u", (unsigned int) c);
|
snprintf(result, 16, "%lu", (unsigned long) c);
|
||||||
PG_RETURN_CSTRING(result);
|
PG_RETURN_CSTRING(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user