g_unichar_to_utf8 creates invalid UTF-8
According to documentation g_unichar_to_utf8 produces a string where "The value is a NUL terminated UTF-8 string." This does not seem to be the case for some unichar values. Here is a sample program:
#include<glib.h>
#include<stdio.h>
int main(int argc, char **argv) {
char buf[10];
memset(buf, 0xff, 10); // Filling with 0 works, this does not.
g_unichar_to_utf8(0xbb, buf);
if(g_utf8_validate(buf, -1, NULL)) {
printf("Output is valid utf-8.\n");
return 0;
} else {
printf("Output is NOT valid utf-8.\n");
return 1;
}
}
The input is the guillement character (») and the output is not valid UTF-8. If you change the memset
to fill the buffer with zeros, the output is valid. This would indicate that the function does not write the zero terminator in its proper place.