[netcdfgroup] a possible problem in libsrc/posixio.c

Hi people behind netcdf,

there is a little problem in libsrc/posixio.c
(I think).

In px_pgout, you do:
-----
        if(write(nciop->fd, vp, extent) != (ssize_t) extent)
        {
                return errno;
        }
-----
Problem is 'write' may write less bytes than you request.
In which case errno will be ENOERR, so the caller of px_pgout
thinks everything went fine, 'extent' bytes were written
to the file.
On the next call to px_pgout, the following assert will fail.
-----
assert(*posp == OFF_NONE || *posp == lseek(nciop->fd, 0, SEEK_CUR));
-----
because the previous call didn't update posp. (Or other functions
where a similar assert is done will fail.)

You should loop the write, something like
(adapt to match your taste):
-----
  count = 0;
  while ((n = write(XXX)) != -1) {
    count += n;
    if (count == extent) break;
  }
  if (n == -1) return errno;
-----

In fact, I had a program that writes into more
than 500 netcdf files and an error popped up
after a while, stating that an assertion at
line 251 failed. I think the disk was more or
less full when that happened. And I strongly
suspect the code of px_pgout to be the one
that triggers this failed assertion. It would
be okay to get an error like "disk full" but
this assertion's failure is weird.

This is speculation of course, and it will be
hard for me to reproduce the error, so if on
your side you are convinced this is a non-issue,
that's fine. But POSIX says a 'write' may write
less bytes than requested, even on a regular
file on a local disk, so I still think your code
lacks a few tests.

Best regards,
Cédric.



  • 2009 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: