summaryrefslogtreecommitdiff
path: root/sql/item_strfunc.cc
diff options
context:
space:
mode:
authorunknown <jimw@mysql.com>2005-11-23 16:49:07 -0800
committerunknown <jimw@mysql.com>2005-11-23 16:49:07 -0800
commit046f82e3a52e212cb50f3d32497030fe317d9b25 (patch)
treee6983101192aea260faa9fcefcfd33221f07b792 /sql/item_strfunc.cc
parent2474fcc1c7420a321bae9660fb9e9c7c8c0e9733 (diff)
downloadmariadb-git-046f82e3a52e212cb50f3d32497030fe317d9b25.tar.gz
Fix possible corruption of results from SUBSTRING_INDEX(). (Bug #14676)
mysql-test/r/func_str.result: Add new results mysql-test/t/func_str.test: Add new test sql/item_strfunc.cc: Mark tmp_value in Item_func_substr_index as const so that we don't overwrite the contents of another String when getting the delimiter. Fix typo in variable names (delimeter -> delimiter).
Diffstat (limited to 'sql/item_strfunc.cc')
-rw-r--r--sql/item_strfunc.cc38
1 files changed, 22 insertions, 16 deletions
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 1e8fe2e695f..9f17e978b39 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -1094,9 +1094,9 @@ void Item_func_substr_index::fix_length_and_dec()
String *Item_func_substr_index::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
- String *res =args[0]->val_str(str);
- String *delimeter =args[1]->val_str(&tmp_value);
- int32 count = (int32) args[2]->val_int();
+ String *res= args[0]->val_str(str);
+ String *delimiter= args[1]->val_str(&tmp_value);
+ int32 count= (int32) args[2]->val_int();
uint offset;
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
@@ -1105,8 +1105,8 @@ String *Item_func_substr_index::val_str(String *str)
return 0;
}
null_value=0;
- uint delimeter_length=delimeter->length();
- if (!res->length() || !delimeter_length || !count)
+ uint delimiter_length= delimiter->length();
+ if (!res->length() || !delimiter_length || !count)
return &my_empty_string; // Wrong parameters
res->set_charset(collation.collation);
@@ -1114,11 +1114,11 @@ String *Item_func_substr_index::val_str(String *str)
#ifdef USE_MB
if (use_mb(res->charset()))
{
- const char *ptr=res->ptr();
- const char *strend = ptr+res->length();
- const char *end=strend-delimeter_length+1;
- const char *search=delimeter->ptr();
- const char *search_end=search+delimeter_length;
+ const char *ptr= res->ptr();
+ const char *strend= ptr+res->length();
+ const char *end= strend-delimiter_length+1;
+ const char *search= delimiter->ptr();
+ const char *search_end= search+delimiter_length;
int32 n=0,c=count,pass;
register uint32 l;
for (pass=(count>0);pass<2;++pass)
@@ -1133,7 +1133,7 @@ String *Item_func_substr_index::val_str(String *str)
if (*i++ != *j++) goto skip;
if (pass==0) ++n;
else if (!--c) break;
- ptr+=delimeter_length;
+ ptr+= delimiter_length;
continue;
}
skip:
@@ -1155,7 +1155,7 @@ String *Item_func_substr_index::val_str(String *str)
}
else /* return right part */
{
- ptr+=delimeter_length;
+ ptr+= delimiter_length;
tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
}
}
@@ -1166,9 +1166,9 @@ String *Item_func_substr_index::val_str(String *str)
{
if (count > 0)
{ // start counting from the beginning
- for (offset=0 ;; offset+=delimeter_length)
+ for (offset=0; ; offset+= delimiter_length)
{
- if ((int) (offset=res->strstr(*delimeter,offset)) < 0)
+ if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
return res; // Didn't find, return org string
if (!--count)
{
@@ -1189,7 +1189,7 @@ String *Item_func_substr_index::val_str(String *str)
address space less than where the found substring is located
in res
*/
- if ((int) (offset=res->strrstr(*delimeter,offset)) < 0)
+ if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
return res; // Didn't find, return org string
/*
At this point, we've searched for the substring
@@ -1197,13 +1197,19 @@ String *Item_func_substr_index::val_str(String *str)
*/
if (!++count)
{
- offset+=delimeter_length;
+ offset+= delimiter_length;
tmp_value.set(*res,offset,res->length()- offset);
break;
}
}
}
}
+ /*
+ We always mark tmp_value as const so that if val_str() is called again
+ on this object, we don't disrupt the contents of tmp_value when it was
+ derived from another String.
+ */
+ tmp_value.mark_as_const();
return (&tmp_value);
}