This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 46d9423f24d [Fix](sql function) memory overflow to the left of string 
address when do_money_format has small negative value #36226 (#37871)
46d9423f24d is described below

commit 46d9423f24d7a5239c0218a57f5b09f6808ef48e
Author: zhiqiang <[email protected]>
AuthorDate: Fri Jul 19 15:58:49 2024 +0800

    [Fix](sql function) memory overflow to the left of string address when 
do_money_format has small negative value #36226 (#37871)
    
    cherry pick from #36226
    
    Co-authored-by: sparrow <[email protected]>
---
 be/src/vec/functions/function_string.h      | 5 ++++-
 be/test/vec/function/function_math_test.cpp | 3 ++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/be/src/vec/functions/function_string.h 
b/be/src/vec/functions/function_string.h
index 6b7bfca9808..48e524079d3 100644
--- a/be/src/vec/functions/function_string.h
+++ b/be/src/vec/functions/function_string.h
@@ -2515,7 +2515,7 @@ static StringRef do_money_format(FunctionContext* 
context, const string& value)
     if (!is_positive) {
         *result_data = '-';
     }
-    for (int i = value.size() - 4, j = result_len - 4; i >= 0; i = i - 3, j = 
j - 4) {
+    for (int i = value.size() - 4, j = result_len - 4; i >= 0; i = i - 3) {
         *(result_data + j) = *(value.data() + i);
         if (i - 1 < 0) {
             break;
@@ -2527,6 +2527,9 @@ static StringRef do_money_format(FunctionContext* 
context, const string& value)
         *(result_data + j - 2) = *(value.data() + i - 2);
         if (j - 3 > 1 || (j - 3 == 1 && is_positive)) {
             *(result_data + j - 3) = ',';
+            j -= 4;
+        } else {
+            j -= 3;
         }
     }
     memcpy(result_data + result_len - 3, value.data() + value.size() - 3, 3);
diff --git a/be/test/vec/function/function_math_test.cpp 
b/be/test/vec/function/function_math_test.cpp
index 3637066eef2..47fbe143f48 100644
--- a/be/test/vec/function/function_math_test.cpp
+++ b/be/test/vec/function/function_math_test.cpp
@@ -509,7 +509,8 @@ TEST(MathFunctionTest, money_format_test) {
         InputTypeSet input_types = {TypeIndex::Float64};
         DataSet data_set = {{{Null()}, Null()},
                             {{DOUBLE(17014116.67)}, VARCHAR("17,014,116.67")},
-                            {{DOUBLE(-17014116.67)}, 
VARCHAR("-17,014,116.67")}};
+                            {{DOUBLE(-17014116.67)}, 
VARCHAR("-17,014,116.67")},
+                            {{DOUBLE(-123.45)}, VARCHAR("-123.45")}};
 
         check_function<DataTypeString, true>(func_name, input_types, data_set);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to