Wrong null termination of UTF16 string generated by g_convert
Submitted by Frederic Danis
Link to original bug (#421104)
Description
Please describe the problem: When using g_convert to convert UTF8 string into UTF16, the null termination is not set properly to 2 '\0' bytes, but only the last but one is set to '\0'. Problem only occurs if the last one is not at '\0' before allocation.
Steps to reproduce:
- Call g_convert to convert UTF8 string to UTF16BE
- Check last 2 bytes of the newly allocated string ( (new_string + bytes_written) and (new_string + bytes_written + 1) )
Actual results: Sometimes the null termination is not set to 2 '\0' bytes.
Expected results: Null termination is UTF16 one.
Does this happen every time? No, depending on memory value before allocation.
Other information: A simple workaround is to allocate a new buffer of bytes_written + 2, copy converted string and set to '\0' last 2 bytes, then free converted string.
I think that g_convert_with_iconv (called by g_convert) should set to '\0' all bytes between outp (not just *outp='\0') and the end of the allocated buffer.
Version: 2.8.x