The following bug has been logged on the website:
Bug reference: 16457
Logged by: ChiJin Zhou
Email address: tlock.chijin@gmail.com
PostgreSQL version: 12.3
Operating system: ubuntu 18.04
Description:
REPRODUCTION && ERROR MESSAGE:
compile the postgres project with UndefinedBehaviorSanitizer and start a
start a server with "postgress" (command: "./postgres -Ddata -p23333"), and
Ubsan reported an error message:
sortsupport.c:60:9: runtime error: implicit conversion from type 'Datum'
(aka 'unsigned long') of value 18446744073709551615 (64-bit, unsigned) to
type 'int' changed the value to -1 (32-bit, signed)
#0 0xf3828d in comparison_shim
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/backend/utils/sort/sortsupport.c:60:9
#1 0x20a2793 in ApplySortComparator.5353
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/backend/commands/../../../src/include/utils/sortsupport.h:224:13
#2 0x20a1c49 in compare_scalars
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/backend/commands/analyze.c:2711:12
#3 0xa9a6bb in med3.26971
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/port/qsort_arg.c:108:4
#4 0xa98d65 in qsort_arg
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/port/qsort_arg.c:158:9
#5 0x2094869 in compute_scalar_stats
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/backend/commands/analyze.c:2284:3
#6 0x2062fb6 in do_analyze_rel
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/backend/commands/analyze.c:541:4
#7 0x2053891 in analyze_rel
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/backend/commands/analyze.c:260:3
#8 0x204be25 in vacuum
/root/chijin_workshop/postgres-new/postgres_source_enhanced/src/backend/commands/vacuum.c:413:5
SUMMARY: UndefinedBehaviorSanitizer:
implicit-signed-integer-truncation-or-sign-change sortsupport.c:60:9
ROOT CAUSE CODE
The root cause code and explanations are shown bellow (sortsupport.c:60).
static int
comparison_shim(Datum x, Datum y, SortSupport ssup)
{
SortShimExtra *extra = (SortShimExtra *) ssup->ssup_extra;
Datum result;
extra->fcinfo.args[0].value = x;
extra->fcinfo.args[1].value = y;
/* just for paranoia's sake, we reset isnull each time */
extra->fcinfo.isnull = false;
result = FunctionCallInvoke(&extra->fcinfo);
<---------- the result value is usigned 64 bit
/* Check for null result, since caller is clearly not expecting one
*/
if (extra->fcinfo.isnull)
elog(ERROR, "function %u returned NULL",
extra->flinfo.fn_oid);
return result; <----------- the return value is
signed 32 bit
}