* Question about C sscanf and unicode
@ 2020-09-14 17:58 jkjdll
2020-09-14 19:45 ` Bruno Haible
0 siblings, 1 reply; 2+ messages in thread
From: jkjdll @ 2020-09-14 17:58 UTC (permalink / raw)
To: bug-gnulib
Hi,
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?
Or, if it doesn't exist, an alternative method to be able to solve
this issue? For the moment I see two possibilities:
- As the code is already using PCRE, replace all space chars by the
simple 0x20, temporarily, while seeing if we can replace the sscanf
eventually by regexps.
- Convert the input string to wchar and use swscanf instead.
If you have any input on the above, I'd appreciate it very much.
Thank you in advance,
--josé
[1] https://github.com/hypermail-project/hypermail
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Question about C sscanf and unicode
2020-09-14 17:58 Question about C sscanf and unicode jkjdll
@ 2020-09-14 19:45 ` Bruno Haible
0 siblings, 0 replies; 2+ messages in thread
From: Bruno Haible @ 2020-09-14 19:45 UTC (permalink / raw)
To: bug-gnulib, Jose Kahan
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-09-14 19:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-14 17:58 Question about C sscanf and unicode jkjdll
2020-09-14 19:45 ` Bruno Haible
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).