Skip to content

gunicode: Fix an UB in gutf8.c and utf8-pointer test

nightuser requested to merge nightuser/glib:fix-utf8-pointer-test into master

In glib/gutf8.c there was an UB in function g_utf8_find_prev_char when p == str. In this case we substract one from p and now p points to a location outside of the boundary of str. It's a UB by the standard. Since this function are meant to be fast, we don't check the boundary conditions.

Fix glib/tests/utf8-pointer test. It failed due to the UB described above and aggressive optimisation when -O2 and LTO are enabled. Some compilers (e.g. GCC with major version >= 8) create an optimised version of g_utf8_find_prev_char with the first argument fixed and stored somewhere else (with a different pointer). It can be solved with either marking str as volatile or creating a copy of str in memory. We choose the second approach since it's more explicit solution.

Add additional checks to glib/tests/utf8-pointer test.

Closes #1917 (closed)

P.S. Due to the gitlab-related errors with a previous (!1198 (closed)) merge request, I have to create a new one. Most of the issues described there are addressed now.

Edited by nightuser

Merge request reports