]> Kevux Git Server - fll/commitdiff
Bugfix: Invalid seeked value on failure in f_file_seek().
authorKevin Day <Kevin@kevux.org>
Wed, 4 Mar 2026 04:41:14 +0000 (22:41 -0600)
committerKevin Day <Kevin@kevux.org>
Wed, 4 Mar 2026 04:44:51 +0000 (22:44 -0600)
The `lseek()` can return a negative value.

Use a local variable to get the `lseek()` value.
This then allows for the `seeked` to be NULL.

Change the `seeked` parameter to be optional and when set to NULL then it is not assigned.
This neither breaks API nor ABI.

The `seeked` value is only assigned when the result is `-1`.
The POSIX standard is inconsistent about negative return values.
In the case of `lseek()`, only `-1` is technically an error.

level_0/f_file/c/file.c
level_0/f_file/c/file.h
level_0/f_file/data/documentation/man/man3/f_file_seek.3
level_0/f_file/tests/unit/c/test-file-seek.c

index cbd55e1101ce90eeb2f227dd971e40500cc49d52..f4fa12b2091989dd31a12f1f0d629a8997bf0ef7 100644 (file)
@@ -1823,14 +1823,13 @@ extern "C" {
   f_status_t f_file_seek(const f_file_t file, const int whence, const off_t offset, off_t * const seeked) {
     #ifndef _di_level_0_parameter_checking_
       if (whence < 0) return F_status_set_error(F_parameter);
-      if (!seeked) return F_status_set_error(F_parameter);
     #endif // _di_level_0_parameter_checking_
 
     if (file.id == -1) return F_file_descriptor_not;
 
-    *seeked = lseek(file.id, offset, whence);
+    const int result = lseek(file.id, offset, whence);
 
-    if (*seeked < 0) {
+    if (result == -1) {
       if (errno == EBADF) return F_status_set_error(F_file_descriptor_not);
       if (errno == EINVAL) return F_status_set_error(F_parameter);
       if (errno == ENXIO) return F_status_set_error(F_bound_not);
@@ -1840,6 +1839,8 @@ extern "C" {
       return F_status_set_error(F_failure);
     }
 
+    *seeked = result;
+
     return F_okay;
   }
 #endif // _di_f_file_seek_
index 5ca80bc149852d76bdc660d075051b2ecc24bfcf..151a108b1d294ea53f587efe35c66edd25030fca 100644 (file)
@@ -1964,9 +1964,11 @@ extern "C" {
  * @param offset
  *   The offset to use, based off of whence.
  * @param seeked
- *   This gets update to represent the total amount seeked.
+ *   (optional) This gets update to represent the total amount seeked.
  *   To be compared against offset.
  *
+ *   Set to NULL to not use.
+ *
  * @return
  *   F_okay on success.
  *   F_file_descriptor_not if file.id is -1.
index e3f0c55e26a5122ec8db1ae0096fc2106cc453d8..e535195da6cbb5b9b0ec51ee5feaf5b6a1255990 100644 (file)
@@ -33,6 +33,7 @@ The offset to use, based off of whence.
 .TP
 .B seeked
 This gets update to represent the total amount seeked. To be compared against offset.
+(optional) This gets update to represent the total amount seeked. To be compared against offset. Set to NULL to not use.
 
 .SH STRUCTURES
 .SS ""
index 0241705524293eb11d62531a9adaf13ff7d02cb2..39589a00492fb99146216ecbfb5623c6ffd70fb5 100644 (file)
@@ -56,12 +56,6 @@ void test__f_file_seek__parameter_checking(void **state) {
 
     assert_int_equal(status, F_status_set_error(F_parameter));
   }
-
-  {
-    const f_status_t status = f_file_seek(file, 0, 0, 0);
-
-    assert_int_equal(status, F_status_set_error(F_parameter));
-  }
 }
 
 void test__f_file_seek__returns_file_descriptor_not(void **state) {