From 80606727e574f78af00832a7d6d78539ce73cf4d Mon Sep 17 00:00:00 2001 From: Artem Fadeev Date: Fri, 23 Aug 2024 14:47:13 +0300 Subject: [PATCH] Fix svace warnings Fixed arithmetics in check_dsa_file_size to avoid server startup failure when aqo.dsm_size_max in bytes overflows signed integer. Updated corresponding tap-test. Two unreachable paths were removed. (cherry-picked from master with a minor change in tap-test) --- cardinality_hooks.c | 3 --- storage.c | 4 +--- t/004_dsm_size_max.pl | 8 +++++++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cardinality_hooks.c b/cardinality_hooks.c index 888fe717..ae6dff5e 100644 --- a/cardinality_hooks.c +++ b/cardinality_hooks.c @@ -447,9 +447,6 @@ aqo_estimate_num_groups(PlannerInfo *root, List *groupExprs, /* It is unclear that to do in situation of such kind. Just report it */ elog(WARNING, "AQO is in the middle of the estimate_num_groups_hook chain"); - if (groupExprs == NIL) - return 1.0; - old_ctx_m = MemoryContextSwitchTo(AQOPredictMemCtx); predicted = predict_num_groups(root, subpath, groupExprs, &fss); diff --git a/storage.c b/storage.c index a11f16f4..10b7cfc6 100644 --- a/storage.c +++ b/storage.c @@ -968,8 +968,6 @@ aqo_get_file_size(const char *filename) ereport(LOG, (errcode_for_file_access(), errmsg("could not read file \"%s\": %m", filename))); - if (file) - FreeFile(file); unlink(filename); return -1; } @@ -981,7 +979,7 @@ check_dsa_file_size(void) long data_size = aqo_get_file_size(PGAQO_DATA_FILE); if (qtext_size == -1 || data_size == -1 || - qtext_size + data_size >= dsm_size_max * 1024 * 1024) + ((unsigned long) qtext_size + (unsigned long) data_size) >> 20 >= dsm_size_max) { elog(ERROR, "aqo.dsm_size_max is too small"); } diff --git a/t/004_dsm_size_max.pl b/t/004_dsm_size_max.pl index 26898b79..8b7f8e62 100644 --- a/t/004_dsm_size_max.pl +++ b/t/004_dsm_size_max.pl @@ -5,7 +5,7 @@ use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; -use Test::More tests => 5; +use Test::More tests => 6; my $node = PostgreSQL::Test::Cluster->new('aqotest'); $node->init; @@ -58,6 +58,12 @@ $node->psql('postgres', 'select * from aqo_reset();'); $node->stop(); +# 3000mb (more than 2*31 bytes) overflows 4-byte signed int +$node->append_conf('postgresql.conf', 'aqo.dsm_size_max = 3000'); +is($node->start(fail_ok => 1), 1, "Large aqo.dsm_size_max doesn't cause integer overflow"); +$node->stop(); + + my $regex; $long_string = 'a' x 100000; $regex = qr/.*WARNING: \[AQO\] Not enough DSA\. AQO was disabled for this query/;