Bug 18799

Summary: iconv_prog exit status with -c is inconsistent
Product: glibc Reporter: Benjamin Herr <ben>
Component: localeAssignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: normal CC: drepper.fsp
Priority: P2 Flags: fweimer: security-
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

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.