mirror of
https://github.com/postgres/postgres.git
synced 2025-06-25 01:02:05 +03:00
Use new overflow aware integer operations.
A previous commit added inline functions that provide fast(er) and correct overflow checks for signed integer math. Use them in a significant portion of backend code. There's more to touch in both backend and frontend code, but these were the easily identifiable cases. The old overflow checks are noticeable in integer heavy workloads. A secondary benefit is that getting rid of overflow checks that rely on signed integer overflow wrapping around, will allow us to get rid of -fwrapv in the future. Which in turn slows down other code. Author: Andres Freund Discussion: https://postgr.es/m/20171024103954.ztmatprlglz3rwke@alap3.anarazel.de
This commit is contained in:
@ -21,6 +21,7 @@
|
||||
#include "access/tuptoaster.h"
|
||||
#include "catalog/pg_collation.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "common/int.h"
|
||||
#include "common/md5.h"
|
||||
#include "lib/hyperloglog.h"
|
||||
#include "libpq/pqformat.h"
|
||||
@ -1047,8 +1048,7 @@ text_overlay(text *t1, text *t2, int sp, int sl)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_SUBSTRING_ERROR),
|
||||
errmsg("negative substring length not allowed")));
|
||||
sp_pl_sl = sp + sl;
|
||||
if (sp_pl_sl <= sl)
|
||||
if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("integer out of range")));
|
||||
@ -2950,8 +2950,7 @@ bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_SUBSTRING_ERROR),
|
||||
errmsg("negative substring length not allowed")));
|
||||
sp_pl_sl = sp + sl;
|
||||
if (sp_pl_sl <= sl)
|
||||
if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("integer out of range")));
|
||||
@ -5279,13 +5278,13 @@ text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
|
||||
|
||||
while (*cp >= '0' && *cp <= '9')
|
||||
{
|
||||
int newval = val * 10 + (*cp - '0');
|
||||
int8 digit = (*cp - '0');
|
||||
|
||||
if (newval / 10 != val) /* overflow? */
|
||||
if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
|
||||
unlikely(pg_add_s32_overflow(val, digit, &val)))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("number is out of range")));
|
||||
val = newval;
|
||||
ADVANCE_PARSE_POINTER(cp, end_ptr);
|
||||
found = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user