List:Internals« Previous MessageNext Message »
From:MARK CALLAGHAN Date:October 14 2009 4:19pm
Subject:Re: String:c_ptr makes me unhappy
View as plain text  
On Wed, Oct 14, 2009 at 9:13 AM, Michael Widenius <monty@stripped> wrote:
>
> Hi!
>
>>>>>> "MARK" == MARK CALLAGHAN <mdcallag@stripped> writes:

>
> MARK> 2) Change the name of c_ptr as it doesn't return a C pointer to a
> MARK> string. If it did, the result would be nul terminated and there would
> MARK> be no need for c_ptr_unsafe.
>
> c_ptr does return a pointer to a \0 terminated string. It does however
> don't know if the last byte is initialized memory or not, which is a
> problem for valgrind but not normally for MySQL as Strings is mainly
> used for thread specific memory.

If you use it in that way then this code has a race. When
Ptr[str_length] == 0, it doesn't realloc and nul-terminate the string.
But Ptr[str_length] might not have been allocated for this string. So
it Ptr[str_length] might be 0 when the check is made and then not 0
when this returns.

  inline char *c_ptr()
  {
    if (!Ptr || Ptr[str_length])                /* Should be safe */
      (void) realloc(str_length);
    return Ptr;
  }

-- 
Mark Callaghan
mdcallag@stripped
Thread
String:c_ptr makes me unhappyMARK CALLAGHAN14 Oct
  • re: String:c_ptr makes me unhappyMichael Widenius14 Oct
    • Re: String:c_ptr makes me unhappyMARK CALLAGHAN14 Oct
      • Re: String:c_ptr makes me unhappyMichael Widenius14 Oct
        • Re: String:c_ptr makes me unhappyKristian Nielsen15 Oct
          • Re: String:c_ptr makes me unhappyMichael Widenius15 Oct