Bug 18799 - iconv_prog exit status with -c is inconsistent
Summary: iconv_prog exit status with -c is inconsistent
Status: NEW
Alias: None
Product: glibc
Classification: Unclassified
Component: locale (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-08-10 16:20 UTC by Benjamin Herr
Modified: 2015-08-27 22:29 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benjamin Herr 2015-08-10 16:20:51 UTC
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/iconv.html , which looks reasonably authoritative, says

> -c
>    Omit any characters that are invalid in the codeset of the input file
>    from the output.
>    [...]
>    The presence or absence of -c shall not affect the exit status of iconv.

I believe this means that invalid input sequences should be silently ignored, but the exit status should still report failure if any were encountered.

iconv_prog.c currently fails to implement that, though the attempt is made: It records when an invalid input sequence has been encountered and ignored, but it clobbers the variable if there was any input in the current buffer that has been successfully converted and needs to be written out (in the process_block function):

      if (n == (size_t) -1 && omit_invalid && errno == EILSEQ)
        {
          ret = 1;
      [...]
      if (outptr != outbuf)
        {
          ret = write_output (outbuf, outptr, output, output_file);

As a result, we can observe the following when telling iconv_prog to interpret 0xff as an ASCII character:

    $ echo -n $'OK \xff' | iconv -c -f ascii > /dev/null; echo $?
    0

    $ echo -n $'\xff OK' | iconv -c -f ascii > /dev/null; echo $?
    0

    $ echo -n $'\xff' | iconv -c -f ascii > /dev/null; echo $?
    1

    $ (head -c 32769 /dev/zero; echo -n $'\xff') | iconv -c -f ascii > /dev/null; echo $?
    1

I believe it should exit with non-zero exit status in all of these situations.