Bug 207544 - [libc] mbtowc(3) fails to set errno(2) for incomplete characters
Summary: [libc] mbtowc(3) fails to set errno(2) for incomplete characters
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-standards (Nobody)
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2016-02-27 14:59 UTC by Ingo Schwarze
Modified: 2018-09-14 21:38 UTC (History)
2 users (show)

See Also:


Attachments
patch against lib/libc/locale/mbtowc.c to fix errno handling (708 bytes, patch)
2016-02-27 14:59 UTC, Ingo Schwarze
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ingo Schwarze 2016-02-27 14:59:59 UTC
Created attachment 167481 [details]
patch against lib/libc/locale/mbtowc.c to fix errno handling

If an incomplete character is passed to mbtowc(3), it fails to set errno to EILSEQ, even though that is unambiguously required by POSIX.

Setting errno in that case makes a lot of sense because having a function that can fail and that sets errno in some failure modes but does not set errno in other failure modes would be a terrible idea.  Such a function would be ridiculously complicated to use.  To detect the reason for failure, you would have to:

 - save errno
 - reset errno to zero
 - call the function
 - inspect the return value to detect failure
 - inspect errno to decide about the reason for failure
 - if errno is zero, restore the saved errno

That is completely unreasonable, in particular for a seemingly innocous function like mbtowc(3).  Next to no programmer would get that right in any real-world program.

Note that this bug is very widespread, it also affects Solaris 11 and glibc, and also OpenBSD until i fixed it today.  I will also send a note around to the other systems.

The OpenBSD commit will soon show up here:
http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/locale/mbtowc.c

The attachment contains a patch against FreeBSD-current, mainly to make it even clearer what exactly i mean.  Note that i did not test the patch since i don't have a FreeBSD system - so please test before commit!  Thanks.
Comment 1 Yuri Pankov 2017-12-05 23:56:49 UTC
This is fixed in base r296278.
Comment 2 Pedro F. Giffuni freebsd_committer 2018-09-14 21:38:25 UTC
Committed Tue Mar 1 19:15:34 2016 UTC.

Thanks!