[PATCH] charset: only update length if char conversion succeeded
Dan Williams
dcbw at redhat.com
Tue Aug 15 15:00:19 UTC 2017
On Tue, 2017-08-15 at 10:06 +0200, Aleksander Morgado wrote:
> The methods that convert from a gunichar to a specific encoding may
> update the output 'clen' value unconditionally, regardless of whether
> the conversion is going to be successful or not. Avoid that by
> updating the length only if the conversion was successful.
> ---
>
> Hey,
>
> Looks like we're updating the length unconditionally, even if the
> char conversion fails. This didn't have any implication in the logic
> because it looks like all the callers of this method just ignore the
> returned length (they're interested in the number of unsupported
> chars returned by reference).
>
> Maybe we should just update the method instead to return the number
> of unsupported chars instead and ignore the output length in the
> target encoding?
Yeah, let's just change the function name to something like
mm_charset_can_convert_to() and just make it return a boolean, since
none of the two callers actually use the 'len' return value. Then we
can exit early if there are any unsupported characters too.
Dan
> ---
> src/mm-charsets.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/src/mm-charsets.c b/src/mm-charsets.c
> index fb47b0ae..5cfb172e 100644
> --- a/src/mm-charsets.c
> +++ b/src/mm-charsets.c
> @@ -627,7 +627,8 @@ mm_charset_get_encoded_len (const char *utf8,
>
> if (!e->func (c, p, (end - p), &clen))
> unsupported++;
> - len += clen;
> + else
> + len += clen;
> p = end;
> }
>
> --
> 2.13.3
> _______________________________________________
> ModemManager-devel mailing list
> ModemManager-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/modemmanager-devel
More information about the ModemManager-devel
mailing list