Skip to content

Commit 7fbbd5c

Browse files
author
Oystein Grovlen
committed
WL#15454 Dictionary support for external languages
Post-push fix for valgrind issue. Use snprintf to avoid writing beyond allocated buffer. Change-Id: If0c40163689b827e8962c91563a62ba8e67a46e4
1 parent 2092dbe commit 7fbbd5c

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

sql/sp.cc

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2002, 2022, Oracle and/or its affiliates.
2+
Copyright (c) 2002, 2023, Oracle and/or its affiliates.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License, version 2.0,
@@ -2081,25 +2081,23 @@ static bool strnstr(const char *str, size_t length, const char *substr) {
20812081
20822082
This is only used for external language routines.
20832083
2084-
@param[in] chistics Routine characteristics
2085-
@param[in] sp_body Start of external language routine body.
2086-
@param[in] sp_body_len Length of routine body
2087-
@param[out] quote Buffer to put the dollar quoute in
2084+
@param[in] chistics Routine characteristics
2085+
@param[in] sp_body Start of external language routine body
2086+
@param[in] sp_body_len Length of routine body
2087+
@param[out] quote Buffer to put the dollar quote in
2088+
@param[in] max_quote_len Max length of dollar quote (incl. null term.)
20882089
20892090
@return Length of dollar quote
20902091
*/
20912092
static size_t find_dollar_quote(const st_sp_chistics *chistics,
20922093
const char *sp_body, size_t sp_body_len,
2093-
char *quote) {
2094+
char *quote, size_t max_quote_len) {
20942095
// Default delimiter is $$
2095-
strcpy(quote, "$$");
2096+
snprintf(quote, max_quote_len, "$$");
20962097
if (!strnstr(sp_body, sp_body_len, quote)) return 2;
20972098

20982099
// Try $language$ instead
2099-
quote[0] = '$';
2100-
strncpy(quote + 1, chistics->language.str, chistics->language.length);
2101-
quote[chistics->language.length + 1] = '$';
2102-
quote[chistics->language.length + 2] = '\0';
2100+
snprintf(quote, max_quote_len, "$%s$", chistics->language.str);
21032101
if (!strnstr(sp_body, sp_body_len, quote))
21042102
return chistics->language.length + 2;
21052103

@@ -2109,12 +2107,7 @@ static size_t find_dollar_quote(const st_sp_chistics *chistics,
21092107
ha_checksum chk =
21102108
my_checksum(i++, reinterpret_cast<const unsigned char *>(sp_body),
21112109
std::max((int)sp_body_len, 20));
2112-
String s;
2113-
s.set_int(chk, true, &my_charset_bin);
2114-
quote[0] = '$';
2115-
strncpy(quote + 1, s.c_ptr(), s.length());
2116-
quote[s.length() + 1] = '$';
2117-
quote[s.length() + 2] = '\0';
2110+
snprintf(quote, max_quote_len, "$%u$", chk);
21182111
} while (strnstr(sp_body, sp_body_len, quote));
21192112
return strlen(quote);
21202113
}
@@ -2135,10 +2128,14 @@ static bool create_string(
21352128
const bool is_sql = chistics->language.length == 0 ||
21362129
native_strcasecmp(chistics->language.str, "SQL") == 0;
21372130

2138-
char dollar_quote[66]; // Max column size is 64; add 2 for the dollar chars
2131+
// Max column size is 64; +3 for dollar characters and null terminator
2132+
const size_t max_quote_len = 67;
2133+
char dollar_quote[max_quote_len];
21392134
const size_t dollar_quote_len =
2140-
is_sql ? 0 : find_dollar_quote(chistics, body, bodylen, dollar_quote);
2141-
assert(dollar_quote_len <= 66);
2135+
is_sql ? 0
2136+
: find_dollar_quote(chistics, body, bodylen, dollar_quote,
2137+
max_quote_len);
2138+
assert(dollar_quote_len < max_quote_len);
21422139

21432140
/* Make some room to begin with */
21442141
if (buf->alloc(100 + dblen + 1 + namelen + paramslen + returnslen + bodylen +

0 commit comments

Comments
 (0)