jerryscript icon indicating copy to clipboard operation
jerryscript copied to clipboard

Segmentation fault at `jerry_api_string_to_char_buffer()` for empty string.

Open ILyoan opened this issue 11 years ago • 5 comments

jerry_api_string_to_char_buffer() was designed to return negation of required bytes for buffer when given buffer is not sufficient.

This function can take NULL for buffer to calculate required bytes to allocate buffer for a string. But if the string is actually empty string ('') then this will result segmentation fault.

  ssize_t ret_val = ecma_string_to_utf8_string (string_p, (lit_utf8_byte_t *) buffer_p, buffer_size);
  if (ret_val >= 0)
  {
    buffer_p[ret_val] = 0;
    ret_val++;
  }
  else
  {
    ret_val--;
  }

here, ecma_string_to_utf8_string returns 0, next if statement hold, and then seg fault.

ILyoan avatar Jul 21 '15 06:07 ILyoan

@ILyoan, thanks for reporting, (NULL, 0) case should be processed gracefully.

Maybe, we should also change this function, so it doesn't write '\0' at the end of buffer, because unicode strings may contain 0 in the middle? @ILyoan, @ruben-ayrapetyan what do you think?

sand1k avatar Jul 22 '15 15:07 sand1k

@sand1k, I think removing \0 from end of buffer is good solution, as it would also make API more accurate and clear in relation to possible presence of \0 inside of output character set.

ruben-ayrapetyan avatar Jul 22 '15 19:07 ruben-ayrapetyan

@sand1k I don't mind removing \0 at the end. Actually I didn't know that this API was adding \0 before encounter this segmentation fault.

ILyoan avatar Jul 24 '15 05:07 ILyoan

@sand1k Can we close this?

ILyoan avatar Aug 16 '15 23:08 ILyoan

@ILyoan closing.

egavrin avatar Nov 19 '15 12:11 egavrin