]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/sysfs: use temporary buffer
authorKarel Zak <kzak@redhat.com>
Fri, 6 Jan 2023 14:58:16 +0000 (15:58 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 6 Jan 2023 14:58:16 +0000 (15:58 +0100)
It seems more readable (for humans and static analyzers) and safe
to use temporary buffer than use memmove to create absolute path.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/Makemodule.am
lib/sysfs.c
meson.build

index e94a4c4e03b207e4cc31b35c5d7560aca768a8c1..d8d54f0507d469bbe8e470aa96c24e18bb555aa0 100644 (file)
@@ -181,7 +181,7 @@ if HAVE_CPU_SET_T
 test_sysfs_SOURCES += lib/cpuset.c
 endif
 test_sysfs_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_SYSFS
-test_sysfs_LDADD = $(LDADD)
+test_sysfs_LDADD = $(LDADD) libcommon.la
 
 test_procfs_SOURCES = lib/procfs.c lib/path.c lib/fileutils.c lib/strutils.c
 if HAVE_CPU_SET_T
index dd2637cdd3c901a344fd14a1e87a4628db19fdf2..92a7e290b082e88a26596c4eb5fd05cff645e6b4 100644 (file)
@@ -17,6 +17,7 @@
 #include "all-io.h"
 #include "debug.h"
 #include "strutils.h"
+#include "buffer.h"
 
 static void sysfs_blkdev_deinit_path(struct path_cxt *pc);
 static int  sysfs_blkdev_enoent_redirect(struct path_cxt *pc, const char *path, int *dirfd);
@@ -387,25 +388,29 @@ static char *get_subsystem(char *chain, char *buf, size_t bufsz)
 char *sysfs_blkdev_get_devchain(struct path_cxt *pc, char *buf, size_t bufsz)
 {
        /* read /sys/dev/block/<maj>:<min> symlink */
-       ssize_t sz = ul_path_readlink(pc, buf, bufsz, NULL);
-       const char *prefix;
-       size_t psz = 0;
+       ssize_t ssz;
+       size_t sz = 0;
+       struct ul_buffer tmp = UL_INIT_BUFFER;
+       const char *p;
+       char *res = NULL;
 
-       if (sz <= 0 || sz + sizeof(_PATH_SYS_DEVBLOCK "/") > bufsz)
+       ssz = ul_path_readlink(pc, buf, bufsz, NULL);
+       if (ssz <= 0)
                return NULL;
 
-       sz++;
-       prefix = ul_path_get_prefix(pc);
-       if (prefix)
-               psz = strlen(prefix);
+       if ((p = ul_path_get_prefix(pc)))
+               ul_buffer_append_string(&tmp, p);
 
-       /* create absolute path from the link */
-       memmove(buf + psz + sizeof(_PATH_SYS_DEVBLOCK "/") - 1, buf, sz);
-       if (prefix)
-               memcpy(buf, prefix, psz);
+       ul_buffer_append_string(&tmp, _PATH_SYS_DEVBLOCK "/");
+       ul_buffer_append_data(&tmp, buf, ssz);
 
-       memcpy(buf + psz, _PATH_SYS_DEVBLOCK "/", sizeof(_PATH_SYS_DEVBLOCK "/") - 1);
-       return buf;
+       p = ul_buffer_get_data(&tmp, &sz, NULL);
+       if (p && sz < bufsz) {
+               memcpy(buf, p, sz);
+               res = buf;
+       }
+       ul_buffer_free_data(&tmp);
+       return res;
 }
 
 /*
index 60fff5ba805cb5bfd9c04c40e5b4997d9ae13cfa..fd069c13116799c918749bc3568597899b3831bb 100644 (file)
@@ -2966,6 +2966,7 @@ exe = executable(
   'test_sysfs',
   'lib/sysfs.c',
   'lib/path.c',
+  'lib/buffer.c',
   'lib/fileutils.c',
   have_cpu_set_t ? 'lib/cpuset.c' : [],
   c_args : ['-DTEST_PROGRAM_SYSFS'],