1

I have encountered a situation where after calling lseek, I would get -1 as a return value but errno would remain unset.

From what I understand by reading the documentation for lseek, -1 return value would indicate an error.

Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the beginning of the file. On error, the value (off_t) -1 is returned and errno is set to indicate the error.

I have the following code:

res = lseek(stream->descriptor, offset, whence);
if (res < 0) {
    int j = errno;
    printf("%d %d\n", res, j);
}

which prints -1 0

I have copied the errno value before calling printf just as suggested in this question but it is still set to 0.

Also, I have observed that if I add one more line the error disappears and lseek returns the correct value.

lseek(stream->descriptor, 0, SEEK_CUR);  // keep the cursor as it is
res = lseek(stream->descriptor, offset, whence);
if (res < 0) {
    int j = errno;
    printf("%d %d\n", res, j);
}

Now, if I've understood lseek correctly the line I've added should not do anything, but I may be mistaken since it solves the error in a dirty way.

Long Claw
  • 107
  • 2
  • 6
  • 1
    What are the values of `offset` and `whence`? Since [`lseek()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html) returns an `off_t`, you should probably be using a different conversion specification (but I concede that it's hard to know which — one safe possibility would be to use `%jd` and cast to `intmax_t` (and add `#include `). – Jonathan Leffler Mar 27 '22 at 23:09
  • I've changed it to `printf("%jd %d\n", (intmax_t) res, j);` and i still get `-1 0`. In my case: `offset` is `0` and `whance` is `SEEK_SET` – Long Claw Mar 27 '22 at 23:26
  • 1
    What you report is surprising; I have no explanation. However, we don't have an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) — the same idea by a different name. Therefore we can't reproduce the problem reliably. Which makes it hard for us to deduce what might be going on. Are you sure you're not working with dead pointers, for example? – Jonathan Leffler Mar 27 '22 at 23:28
  • Hmm, yes... I'll try and make one. As for my testing environment: this code's function is inside a dynamic library which is called from main (maybe this can interfere with the os's file offset in some way) – Long Claw Mar 27 '22 at 23:36
  • 1
    Particularly relevant would be the type with which `res` was declared. It should be `off_t`. And following the documentation, the correct test for failure is not `res < 0` but `res == (off_t)-1`. – Nate Eldredge Mar 28 '22 at 00:37
  • For instance, you could get this result if `res` is declared `int` and you successfully seek to offset 4294967295. Also, you did `#include `, correct? – Nate Eldredge Mar 28 '22 at 00:39
  • Which OS are you using? – rici Mar 28 '22 at 02:28

0 Answers0