The compilers on FreeBSD 11 and OpenBSD 7.6 (at least) report give this
warning:

../src/copy-file-data.c:585:7: warning: variable 'result' is used uninitialized 
whenever 'if' condition is false [-Wsometimes-uninitialized]

There is an 'assume (scantype != LSEEK_SCANTYPE);' in line 582,
that is apparently supposed to teach the compiler about the
control flow. But still, even as a human reader I find this code
hard to understand. How about this patch? I have verified that
it silences the warning on both FreeBSD and OpenBSD.

>From 293dc379b3de1001bd94e546655191055973eac4 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Thu, 18 Sep 2025 22:30:49 +0200
Subject: [PATCH] maint: Eliminate clang warning

* src/copy-file-data.c (copy_file_data): Use 'switch' statement instead
of 'if's with nontrivial control flow.
---
 src/copy-file-data.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/src/copy-file-data.c b/src/copy-file-data.c
index 44b542de0..3c5fb4c80 100644
--- a/src/copy-file-data.c
+++ b/src/copy-file-data.c
@@ -571,23 +571,28 @@ copy_file_data (int ifd, struct stat const *ist, off_t ipos, char const *iname,
   intmax_t result;
   off_t hole_size = 0;
 
+  switch (scantype)
+    {
+    case LSEEK_SCANTYPE:
 #ifdef SEEK_HOLE
-  if (scantype == LSEEK_SCANTYPE)
-    result = lseek_copy (ifd, ofd, &buf, buf_size,
-                         ipos, ibytes, &scan_inference, ist->st_size,
-                         make_holes ? x->sparse_mode : SPARSE_NEVER,
-                         x->reflink_mode != REFLINK_NEVER,
-                         iname, oname, &hole_size, debug);
+      result = lseek_copy (ifd, ofd, &buf, buf_size,
+                           ipos, ibytes, &scan_inference, ist->st_size,
+                           make_holes ? x->sparse_mode : SPARSE_NEVER,
+                           x->reflink_mode != REFLINK_NEVER,
+                           iname, oname, &hole_size, debug);
+      break;
 #else
-  assume (scantype != LSEEK_SCANTYPE);
+      abort ();
 #endif
 
-  if (scantype != LSEEK_SCANTYPE)
-    result = sparse_copy (ifd, ofd, &buf, buf_size,
-                          x->reflink_mode != REFLINK_NEVER,
-                          iname, oname, ibytes,
-                          make_holes ? &hole_size : nullptr,
-                          debug);
+    default:
+      result = sparse_copy (ifd, ofd, &buf, buf_size,
+                            x->reflink_mode != REFLINK_NEVER,
+                            iname, oname, ibytes,
+                            make_holes ? &hole_size : nullptr,
+                            debug);
+    break;
+    }
 
   if (0 <= result && 0 < hole_size)
     {
-- 
2.51.0

Reply via email to