Skip to content

Commit 2b24ed8

Browse files
grooverdanspetrunia
authored andcommitted
MDEV-36765: followup 2: st_append_json: handle json_unescape error
Now pushes the OUTOFMEMORY error and ER_JSON_BAD_CHAR as a warning if these resulted in those errors. callers only expected a bool so the prototype was changed. Json_engine_scan::check_and_get_value_scalar failed to handle the error condition so set the *error if an error occured and return the correct value.
1 parent 0dd758e commit 2b24ed8

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

sql/item_func.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ extern "C" /* Bug in BSDI include file */
3535
#include <cmath>
3636

3737

38-
extern int st_append_json(String *s,
39-
CHARSET_INFO *json_cs, const uchar *js, uint js_len);
38+
extern bool st_append_json(String *s,
39+
CHARSET_INFO *json_cs, const uchar *js, uint js_len);
4040
class Item_func :public Item_func_or_sum
4141
{
4242
void sync_with_sum_func_and_with_field(List<Item> &list);

sql/item_jsonfunc.cc

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,34 @@ append_simple(String *s, const uchar *a, size_t a_len)
9898
Appends JSON string to the String object taking charsets in
9999
consideration.
100100
*/
101-
int st_append_json(String *s,
101+
bool st_append_json(String *s,
102102
CHARSET_INFO *json_cs, const uchar *js, uint js_len)
103103
{
104104
int str_len= js_len * s->charset()->mbmaxlen;
105105

106-
if (!s->reserve(str_len, 1024) &&
107-
(str_len= json_unescape(json_cs, js, js + js_len,
106+
if (s->reserve(str_len, 1024))
107+
{
108+
my_error(ER_OUTOFMEMORY, MYF(0), str_len);
109+
return false;
110+
}
111+
112+
if ((str_len= json_unescape(json_cs, js, js + js_len,
108113
s->charset(), (uchar *) s->end(), (uchar *) s->end() + str_len)) > 0)
109114
{
110115
s->length(s->length() + str_len);
111-
return 0;
116+
return false;
117+
}
118+
if (current_thd)
119+
{
120+
if (str_len == JSON_ERROR_OUT_OF_SPACE)
121+
my_error(ER_OUTOFMEMORY, MYF(0), str_len);
122+
else if (str_len == JSON_ERROR_ILLEGAL_SYMBOL)
123+
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
124+
ER_JSON_BAD_CHR, ER_THD(current_thd, ER_JSON_BAD_CHR),
125+
0, "st_append_json", 0);
112126
}
113127

114-
return str_len;
128+
return true;
115129
}
116130

117131

@@ -791,8 +805,12 @@ bool Json_engine_scan::check_and_get_value_scalar(String *res, int *error)
791805
js_len= value_len;
792806
}
793807

794-
795-
return st_append_json(res, json_cs, js, js_len);
808+
if (st_append_json(res, json_cs, js, js_len))
809+
{
810+
*error= 1;
811+
return true;
812+
}
813+
return false;
796814
}
797815

798816

0 commit comments

Comments
 (0)