bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org, Jose Kahan <jkjdll@w3.org>
Subject: Re: Question about C sscanf and unicode
Date: Mon, 14 Sep 2020 21:45:41 +0200	[thread overview]
Message-ID: <2330305.RJ93V7QO3L@omega> (raw)
In-Reply-To: <20200914175847.GA13143@w3.org>

Jose Kahan wrote:
> I am a contributor to an mail hypertext archiving system called
> hypermail [1], which is written in C.
> 
> Recently a bug was raised that one of its parsers had problems
> when the input string had a nbsp. As you may imagine from my subject,
> this is because that parser uses sscanf and the nbsp corresponds
> to UTF-8 U+00A0 character:
> 
>   urlscan = sscanf(inputp, "%255[^] )<>\"\'\n[\t\\]", urlbuff);
> 
> o you know if there's an sscanf function that is UTF-8 aware?

According to POSIX [1], the %l[ directive parses multibyte characters.
If you set the locale to a UTF-8 locale - such as through
  setlocale (LC_CTYPE, "en_US.UTF-8");
- you should be able to achieve this, at least on glibc systems.

However, this is complex code, and I doubt all platforms get this
right correctly. We found 20 bugs in *printf implementations on
various platforms. I wouldn't be surprised if there were 10 bugs
in *scanf implementations, and this part is among the hairiest in
sscanf.

> - Convert the input string to wchar and use swscanf instead.

This is what I would suggest, because
  - swscanf is portable enough [2].
  - Parsing sequences of wide-characters in a wide-character string is
    more likely to be correctly implemented everywhere.

> seeing if we can replace the sscanf eventually by regexps.

Anyone has experience with this?

Bruno

[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html
[2] https://www.gnu.org/software/gnulib/manual/html_node/swscanf.html



      reply	other threads:[~2020-09-14 19:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-14 17:58 Question about C sscanf and unicode jkjdll
2020-09-14 19:45 ` Bruno Haible [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2330305.RJ93V7QO3L@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=jkjdll@w3.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).