summaryrefslogtreecommitdiff
path: root/sql/sql_string.cc
diff options
context:
space:
mode:
authorMartin Hansson <martin.hansson@oracle.com>2011-01-13 08:57:15 +0100
committerMartin Hansson <martin.hansson@oracle.com>2011-01-13 08:57:15 +0100
commit3ef71bfac7ccdf7cb7a3d8f28a1695f0ea7be45f (patch)
tree6cc0a92d31d0e672ac9851fa8ba338d0633a5547 /sql/sql_string.cc
parentf23725f2bb488894f179cbe69f7aa63de8c24bab (diff)
downloadmariadb-git-3ef71bfac7ccdf7cb7a3d8f28a1695f0ea7be45f.tar.gz
Bug#58165: "my_empty_string" gets modified and causes LOAD DATA to fail and
other crashes Some string manipulating SQL functions use a shared string object intended to contain an immutable empty string. This object was used by the SQL function SUBSTRING_INDEX() to return an empty string when one argument was of the wrong datatype. If the string object was then modified by the sql function INSERT(), undefined behavior ensued. Fixed by instead modifying the string object representing the function's result value whenever string manipulating SQL functions return an empty string. Relevant code has also been documented.
Diffstat (limited to 'sql/sql_string.cc')
-rw-r--r--sql/sql_string.cc41
1 files changed, 37 insertions, 4 deletions
diff --git a/sql/sql_string.cc b/sql/sql_string.cc
index a41f4d52101..4f76943bde7 100644
--- a/sql/sql_string.cc
+++ b/sql/sql_string.cc
@@ -58,11 +58,33 @@ bool String::real_alloc(uint32 arg_length)
}
-/*
-** Check that string is big enough. Set string[alloc_length] to 0
-** (for C functions)
-*/
+/**
+ Allocates a new buffer on the heap for this String.
+
+ - If the String's internal buffer is privately owned and heap allocated,
+ one of the following is performed.
+
+ - If the requested length is greater than what fits in the buffer, a new
+ buffer is allocated, data moved and the old buffer freed.
+
+ - If the requested length is less or equal to what fits in the buffer, a
+ null character is inserted at the appropriate position.
+ - If the String does not keep a private buffer on the heap, such a buffer
+ will be allocated and the string copied accoring to its length, as found
+ in String::length().
+
+ For C compatibility, the new string buffer is null terminated.
+
+ @param alloc_length The requested string size in characters, excluding any
+ null terminator.
+
+ @retval false Either the copy operation is complete or, if the size of the
+ new buffer is smaller than the currently allocated buffer (if one exists),
+ no allocation occured.
+
+ @retval true An error occured when attempting to allocate memory.
+*/
bool String::realloc(uint32 alloc_length)
{
uint32 len=ALIGN_SIZE(alloc_length+1);
@@ -196,6 +218,17 @@ bool String::copy()
return FALSE;
}
+/**
+ Copies the internal buffer from str. If this String has a private heap
+ allocated buffer where new data does not fit, a new buffer is allocated
+ before copying and the old buffer freed. Character set information is also
+ copied.
+
+ @param str The string whose internal buffer is to be copied.
+
+ @retval false Success.
+ @retval true Memory allocation failed.
+*/
bool String::copy(const String &str)
{
if (alloc(str.str_length))