Project

General

Profile

Bug #6689

mbtowc(3C) fails to set errno for incomplete characters

Added by Ingo Schwarze almost 4 years ago. Updated almost 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
lib - userland libraries
Start date:
2016-02-27
Due date:
% Done:

100%

Estimated time:
0.50 h
Difficulty:
Bite-size
Tags:

Description

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 FreeBSD and glibc, and also OpenBSD until i fixed it today. I will also send a note around to the other systems.

The OpenBSD patch is here:
http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/locale/mbtowc.c

The attachment contains a patch against illumos-gate -current, mainly to make it even clearer what exactly i mean. Note that i did not test the patch since i don't have an illumos system - so please test before commit! Thanks.


Files

tmp.patch (597 Bytes) tmp.patch patch against lib/libc/port/locale/mbtowc.c to fix errno handling Ingo Schwarze, 2016-02-27 06:09 PM

History

#1

Updated by Yuri Pankov almost 3 years ago

  • Subject changed from mbtowc(3) fails to set errno for incomplete characters to mbtowc(3C) fails to set errno for incomplete characters
  • Status changed from New to In Progress
  • Assignee set to Yuri Pankov
  • % Done changed from 0 to 50
  • Difficulty changed from Medium to Bite-size
  • Tags deleted (needs-triage)
#2

Updated by Electric Monk almost 3 years ago

  • Status changed from In Progress to Closed
  • % Done changed from 50 to 100

git commit 440578c35af1ea0360a52ee814488a4d0bbbc160

commit  440578c35af1ea0360a52ee814488a4d0bbbc160
Author: Ingo Schwarze <schwarze@openbsd.org>
Date:   2017-03-28T04:15:46.000Z

    6689 mbtowc(3C) fails to set errno for incomplete characters
    Reviewed by: Robert Mustacchi <rm@joyent.com>
    Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
    Approved by: Dan McDonald <danmcd@omniti.com>

Also available in: Atom PDF