From: MARK CALLAGHAN Date: October 14 2009 4:19pm Subject: Re: String:c_ptr makes me unhappy List-Archive: http://lists.mysql.com/internals/37373 Message-Id: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Oct 14, 2009 at 9:13 AM, Michael Widenius wrote: > > Hi! > >>>>>> "MARK" == MARK CALLAGHAN 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