Bug 5930 - Improper return value of fwrite in bytes,
Summary: Improper return value of fwrite in bytes,
Status: NEW
Alias: None
Product: uClibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: 0.9.33.2
Hardware: All Linux
: P5 minor
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-02-14 05:32 UTC by upendra
Modified: 2020-02-19 12:15 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description upendra 2013-02-14 05:32:37 UTC
fwrite(buf, sizeof(buf), 1, fp)

if any error occurs such as ENOSPC or EIO, then also fwrite return value 1, but proper error code is set.

but from the man page of fwrite:
fwrite()  return  the number of items successfully read or written (i.e., not the number of characters).  If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

But it can be seen with uclibc 0.9.33.2 the return value is 1 for the above use case in case of failure also.

This issue was not observed with uClibc version 0.9.33.

Also as seen from the code in libc/stdio/_WRITE.c the following part seems to be culpit
 * some hidden buffered crap in the buffer.
 */
 if (errno != EINTR && errno != EAGAIN) {
        /* do we have other "soft" errors? */
           break;
  }

return bufsize;

Apart from other these 2 error it breaks out of the loop and return bufsize .
Comment 1 Bernhard Reutner-Fischer 2013-02-14 09:14:16 UTC
fwrite does return the number of elements written. See libc/stdio/fwrite.c, 
return __stdio_fwrite()/size.

can you please attach your .config (which STDIO_BUFF settings do you use) and it would help if you could also paste an strace -v of your failing test program.

thanks,
Comment 2 upendra 2013-02-14 11:33:12 UTC
(In reply to comment #1)
> fwrite does return the number of elements written. See libc/stdio/fwrite.c, 
> return __stdio_fwrite()/size.
> 
> can you please attach your .config (which STDIO_BUFF settings do you use) and
> it would help if you could also paste an strace -v of your failing test
> program.
> 
> thanks,

Hi
Below are the STDIO_BUFF settings used in .config
# UCLIBC_HAS_STDIO_BUFSIZ_NONE is not set
# UCLIBC_HAS_STDIO_BUFSIZ_256 is not set
# UCLIBC_HAS_STDIO_BUFSIZ_512 is not set
# UCLIBC_HAS_STDIO_BUFSIZ_1024 is not set
# UCLIBC_HAS_STDIO_BUFSIZ_2048 is not set
UCLIBC_HAS_STDIO_BUFSIZ_4096=y
# UCLIBC_HAS_STDIO_BUFSIZ_8192 is not set
UCLIBC_HAS_STDIO_BUILTIN_BUFFER_NONE=y
# UCLIBC_HAS_STDIO_BUILTIN_BUFFER_4 is not set
# UCLIBC_HAS_STDIO_BUILTIN_BUFFER_8 is not set
# UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT is not set
# UCLIBC_HAS_STDIO_GETC_MACRO is not set
# UCLIBC_HAS_STDIO_PUTC_MACRO is not set
UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION=y
UCLIBC_HAS_STDIO_FUTEXES=y

the same settings work with uClibc09.33. version.
But in uClibc 09.33.2 if i comment the following part in _WRITE.c
 if (errno != EINTR && errno != EAGAIN) {
      /* do we have other "soft" errors?*/
      break;
 }
code seems to work fine.
i have just used a normal fwrite function call with following check
if(fwrite(buf, sizeof(buf), 1, fp != 1) {
     printf("error %s\n", strerror(errno));
}

this is the way i checked return value for fwrite.

thanks.
Comment 3 Bernhard Reutner-Fischer 2013-02-14 12:28:42 UTC
(In reply to comment #2)
> (In reply to comment #1)
> > fwrite does return the number of elements written. See libc/stdio/fwrite.c, 
> > return __stdio_fwrite()/size.
> > 
> > can you please attach your .config (which STDIO_BUFF settings do you use) and
> > it would help if you could also paste an strace -v of your failing test
> > program.

> the same settings work with uClibc09.33. version.
> But in uClibc 09.33.2 if i comment the following part in _WRITE.c
>  if (errno != EINTR && errno != EAGAIN) {
>       /* do we have other "soft" errors?*/
>       break;
>  }
> code seems to work fine.
> i have just used a normal fwrite function call with following check
> if(fwrite(buf, sizeof(buf), 1, fp != 1) {
>      printf("error %s\n", strerror(errno));

as you can see in the history, Denys did this to drain the buffer.
what errno do you trip there (that was why i was asking for an strace)?
Comment 4 upendra 2013-02-14 12:41:10 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > (In reply to comment #1)
> > > fwrite does return the number of elements written. See libc/stdio/fwrite.c, 
> > > return __stdio_fwrite()/size.
> > > 
> > > can you please attach your .config (which STDIO_BUFF settings do you use) and
> > > it would help if you could also paste an strace -v of your failing test
> > > program.
> 
> > the same settings work with uClibc09.33. version.
> > But in uClibc 09.33.2 if i comment the following part in _WRITE.c
> >  if (errno != EINTR && errno != EAGAIN) {
> >       /* do we have other "soft" errors?*/
> >       break;
> >  }
> > code seems to work fine.
> > i have just used a normal fwrite function call with following check
> > if(fwrite(buf, sizeof(buf), 1, fp != 1) {
> >      printf("error %s\n", strerror(errno));
> 
> as you can see in the history, Denys did this to drain the buffer.
> what errno do you trip there (that was why i was asking for an strace)?

Thanks... Due to some official reasons i am not able to provide the trace.but i will write a simple code and provide you with the strace for this failure.
The scenario was that I was writing data to sdcard and if i removed the sdcard from device then fwrite should fail but it returns 1. Err no was set to EIO.Same issue for if no space in sdcard ENOSPC also.
Comment 5 Bernhard Reutner-Fischer 2013-02-19 07:31:25 UTC
> Thanks... Due to some official reasons i am not able to provide the trace.but i
> will write a simple code and provide you with the strace for this failure.
> The scenario was that I was writing data to sdcard and if i removed the sdcard
> from device then fwrite should fail but it returns 1. Err no was set to
> EIO.Same issue for if no space in sdcard ENOSPC also.

http://lists.uclibc.org/pipermail/uclibc/2013-February/047524.html
and follow-ups.
Comments?
Comment 6 upendra 2013-02-19 08:26:09 UTC
(In reply to comment #5)
> > Thanks... Due to some official reasons i am not able to provide the trace.but i
> > will write a simple code and provide you with the strace for this failure.
> > The scenario was that I was writing data to sdcard and if i removed the sdcard
> > from device then fwrite should fail but it returns 1. Err no was set to
> > EIO.Same issue for if no space in sdcard ENOSPC also.
> 
> http://lists.uclibc.org/pipermail/uclibc/2013-February/047524.html
> and follow-ups.
> Comments?

Thanks ...Yes  it was the similar test case mentioned in the above link. i treid with the patch attached in the above link and it seems to be working fine.No issue is observed when I applied the patch mentioned in above link.