• Alexander Barkov's avatar
    MDEV-25402 Assertion `!str || str != Ptr' failed in String::copy · 2ed148c8
    Alexander Barkov authored
    The assert inside String::copy() prevents copying from from "str"
    if its own String::Ptr also points to the same memory.
    
    The idea of the assert is that copy() performs memory reallocation,
    and this reallocation can free (and thus invalidate) the memory pointed by Ptr,
    which can lead to further copying from a freed memory.
    
    The assert was incomplete: copy() can free the memory pointed by its Ptr
    only if String::alloced is true!
    
    If the String is not alloced, it is still safe to copy even from
    the location pointed by Ptr.
    
    This scenario demonstrates a safe copy():
      const char *tmp= "123";
      String str1(tmp, 3);
      String str2(tmp, 3);
      // This statement is safe:
      str2.copy(str1->ptr(), str1->length(), str1->charset(), cs_to, &errors);
    
    Inside the copy() the parameter "str" is equal to String::Ptr in this example.
    But it's still ok to reallocate the memory for str2, because str2
    was a constant before the copy() call. Thus reallocation does not
    make the memory pointed by str1->ptr() invalid.
    
    Adjusting the assert condition to allow copying for constant strings.
    2ed148c8
sql_string.cc 33.5 KB