[PATCH] Fixed potential, rare corruption of unicode characters

Dirk Hohndel dirk at hohndel.org
Tue Oct 2 10:13:27 PDT 2012


BTW: documentation of g_utf8_strncpy has this to say:

The src string must be valid UTF-8 encoded text. (Use g_utf8_validate()
on all text before trying to use UTF-8 utility functions with it.)

I guess that's something we should do?

/D

"Lubomir I. Ivanov" <neolit123 at gmail.com> writes:

> From: "Lubomir I. Ivanov" <neolit123 at gmail.com>
>
> In divelist.c:get_string(), when truncating the string to a maximum
> of 60 characters (to be shown in the divelist), make sure we are
> counting in guinchar (sizeof usually 2) instead of gchar (sizeof usually 1).
> Use Glib functions such as g_utf8_strlen() and g_utf8_strncpy() to do that.
>
> This patch fixes the potential problem when truncating a UTF-8 string
> by calculating its length using strlen() in bytes.
>
> For char = 1 byte, if the length returned by strlen() is an odd number
> this means there is at least one single byte length character in there.
>
> But also if the same string has a UTF-8 character at exactly the truncate
> position minus x(probably 1) bytes, we are going to split the bytes
> forming said UTF-8 char resulting in an incorrect string.
>
> Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
> ---
>  divelist.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/divelist.c b/divelist.c
> index e09eca5..910b0c9 100644
> --- a/divelist.c
> +++ b/divelist.c
> @@ -788,12 +788,11 @@ static void get_string(char **str, const char *s)
>  
>  	if (!s)
>  		s = "";
> -	len = strlen(s);
> +	len = g_utf8_strlen(s, -1);
>  	if (len > 60)
>  		len = 60;
> -	n = malloc(len+1);
> -	memcpy(n, s, len);
> -	n[len] = 0;
> +	n = malloc(len * sizeof(gunichar) + 1);
> +	g_utf8_strncpy(n, s, len);
>  	*str = n;
>  }
>  
> -- 
> 1.7.11.msysgit.0
>
> _______________________________________________
> subsurface mailing list
> subsurface at hohndel.org
> http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

-- 
Dirk Hohndel
Intel Open Source Technology Center


More information about the subsurface mailing list