From: Karel Zak Date: Tue, 23 Oct 2012 10:40:39 +0000 (+0200) Subject: misc: make readlink() usage more robust X-Git-Tag: v2.23-rc1~578 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9f51089e7f14f9b6f432a20ca59c0db5e5f16325;p=thirdparty%2Futil-linux.git misc: make readlink() usage more robust Signed-off-by: Karel Zak --- diff --git a/lib/sysfs.c b/lib/sysfs.c index 29bde82d0a..3ac1fe0ca4 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -646,7 +646,7 @@ int sysfs_scsi_get_hctl(struct sysfs_cxt *cxt, int *h, int *c, int *t, int *l) if (cxt->has_hctl) goto done; - len = sysfs_readlink(cxt, "device", buf, sizeof(buf)); + len = sysfs_readlink(cxt, "device", buf, sizeof(buf) - 1); if (len < 0) return len; diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index ae203cab21..c57e78f1f8 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -1045,7 +1045,7 @@ static int get_wholedisk_from_partition_dirent(DIR *dir, const char *dirname, int len; if ((len = readlink_at(dirfd(dir), dirname, - d->d_name, path, sizeof(path))) < 0) + d->d_name, path, sizeof(path) - 1)) < 0) return 0; path[len] = '\0'; @@ -1158,7 +1158,7 @@ static char *devno_to_sysfs_name(dev_t devno, char *devname, char *buf, size_t b return NULL; } - len = readlink(path, buf, buf_size); + len = readlink(path, buf, buf_size - 1); if (len < 0) { warn(_("%s: failed to read link"), path); return NULL; diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c index 820f5dcfaa..33dc88750c 100644 --- a/misc-utils/lslocks.c +++ b/misc-utils/lslocks.c @@ -196,7 +196,7 @@ static char *get_filename_sz(ino_t inode, pid_t pid, size_t *size) continue; if ((len = readlink_at(fd, path, dp->d_name, - sym, sizeof(path))) < 1) + sym, sizeof(sym) - 1)) < 1) goto out; *size = sb.st_size; diff --git a/sys-utils/eject.c b/sys-utils/eject.c index 5d5d7b18b3..e28837155e 100644 --- a/sys-utils/eject.c +++ b/sys-utils/eject.c @@ -837,7 +837,7 @@ static char *get_subsystem(char *chain, char *buf, size_t bufsz) memcpy(chain + len, SUBSYSTEM_LINKNAME, sizeof(SUBSYSTEM_LINKNAME)); /* try if subsystem symlink exists */ - sz = readlink(chain, buf, bufsz); + sz = readlink(chain, buf, bufsz - 1); /* remove last subsystem from chain */ chain[len] = '\0';