lseek(2) problems

VOROSKOI Andras voroskoi at
Sun Jun 1 16:40:21 PDT 2008


Today I've tried to send in Matt's gnulib patch to upstream with more of
less luck.
As for now
compiles fine, but one of the test fails.
According to Bruno it's a bug in out lseek implementation and
contains the following:

The POSIX.1-1990 standard did not specifically prohibit lseek() from
returning a negative offset. Therefore, an application was required to
clear errno prior to the call and check errno upon return to determine
whether a return value of ( off_t)-1 is a negative offset or an
indication of an error condition. The standard developers did not wish
to require this action on the part of a conforming application, and
chose to require that errno be set to [EINVAL] when the resulting file
offset would be negative for a regular file, block special file, or

You can download the testcase if you want, but I also attach a simple
testcase. Let me know what do you think about it.

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#define ASSERT(expr) \
  do                                                                         \
    {                                                                        \
      if (!(expr))                                                           \
        {                                                                    \
          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
          fflush (stderr);                                                   \
          abort ();                                                          \
        }                                                                    \
    }                                                                        \
  while (0)

int main(void) {
	ASSERT (lseek (0, (off_t)-4, SEEK_CUR) == -1);
	ASSERT (errno == EINVAL);
	errno = 0;
	ASSERT (lseek (1, (off_t)-4, SEEK_CUR) == -1);
	ASSERT (errno == EINVAL);
	errno = 0;

More information about the Users mailing list