summaryrefslogtreecommitdiff
path: root/libsysfs
diff options
context:
space:
mode:
authorananth@in.ibm.com <ananth@in.ibm.com>2004-03-12 00:57:36 -0800
committerGreg KH <gregkh@suse.de>2005-04-26 21:35:09 -0700
commit656703759d7d3eac6e8c86f1121cde7dfd6d8cbd (patch)
tree0f8161838607f73afdd30c3c7d3d3256d53ea5ec /libsysfs
parentd1fb871d99db38c7704d8e583ff5e0a00e713837 (diff)
[PATCH] more Libsysfs updates
On Thu, Mar 11, 2004 at 02:36:23PM +0100, Kay Sievers wrote: > On Thu, 2004-03-11 at 15:02, Ananth N Mavinakayanahalli wrote: > > On Thu, Mar 11, 2004 at 02:04:36PM +0100, Kay Sievers wrote: > > > On Thu, Mar 11, 2004 at 11:53:50AM +0500, Ananth N Mavinakayanahalli wrote: > > > > > > > +#define safestrcpy(to, from) strncpy(to, from, sizeof(to)-1) > > > > +#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1) > > > > > > These strings are not terminated with '\0' if from is longer than > > > the sizeof to. > > > > Did not do it on purpose as the "to" elements are either calloc'd or memset to > > '0' explicitly in the library. Thats the reason I mentioned "scaled down" :) > > Ahh, sounds good. > > > > > +#define safestrncpy(to, from, maxsize) \ > > > > +do { \ > > > > + to[maxsize-1] = '\0'; \ > > > > + strncpy(to, from, maxsize-1); \ > > > > +} while (0) > > > > + > > > > +#define safestrncat(to, from, maxsize) \ > > > > +do { \ > > > > + to[maxsize-1] = '\0'; \ > > > > + strncat(to, from, maxsize - strlen(to)-1); \ > > > > +} while (0) > > > > > > We all expect a similar behavior like strncat/strncpy according to the > > > names, but these macros are limiting by the target size and do not limit > > > the count of chars copied. > > > This is confusing I think and suggest using a different name like > > > 'safestrcopymax()' or something. > > > > Good point.. will make the change > > Nice. I've had these *n* names too and I forgot about the logic and only > 10 days later I introduced a ugly bug cause I can't limit the count of > copied chars :) Inlined is the patch for this... applies on the earlier _BIG_ patch.
Diffstat (limited to 'libsysfs')
-rw-r--r--libsysfs/sysfs/libsysfs.h12
-rw-r--r--libsysfs/sysfs_bus.c2
-rw-r--r--libsysfs/sysfs_class.c16
-rw-r--r--libsysfs/sysfs_device.c2
-rw-r--r--libsysfs/sysfs_dir.c6
-rw-r--r--libsysfs/sysfs_driver.c16
-rw-r--r--libsysfs/sysfs_utils.c26
7 files changed, 40 insertions, 40 deletions
diff --git a/libsysfs/sysfs/libsysfs.h b/libsysfs/sysfs/libsysfs.h
index cbde2f5783..11cffae045 100644
--- a/libsysfs/sysfs/libsysfs.h
+++ b/libsysfs/sysfs/libsysfs.h
@@ -32,16 +32,16 @@
#define safestrcpy(to, from) strncpy(to, from, sizeof(to)-1)
#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1)
-#define safestrncpy(to, from, maxsize) \
+#define safestrcpymax(to, from, max) \
do { \
- to[maxsize-1] = '\0'; \
- strncpy(to, from, maxsize-1); \
+ to[max-1] = '\0'; \
+ strncpy(to, from, max-1); \
} while (0)
-#define safestrncat(to, from, maxsize) \
+#define safestrcatmax(to, from, max) \
do { \
- to[maxsize-1] = '\0'; \
- strncat(to, from, maxsize - strlen(to)-1); \
+ to[max-1] = '\0'; \
+ strncat(to, from, max - strlen(to)-1); \
} while (0)
/*
diff --git a/libsysfs/sysfs_bus.c b/libsysfs/sysfs_bus.c
index bff7f78f05..9ca23c85d8 100644
--- a/libsysfs/sysfs_bus.c
+++ b/libsysfs/sysfs_bus.c
@@ -384,7 +384,7 @@ int sysfs_find_driver_bus(const char *driver, char *busname, size_t bsize)
if (drivers != NULL) {
dlist_for_each_data(drivers, curdrv, char) {
if (strcmp(driver, curdrv) == 0) {
- safestrncpy(busname,
+ safestrcpymax(busname,
bus, bsize);
sysfs_close_list(drivers);
sysfs_close_list(buslist);
diff --git a/libsysfs/sysfs_class.c b/libsysfs/sysfs_class.c
index 7a696355ca..59ef0be48e 100644
--- a/libsysfs/sysfs_class.c
+++ b/libsysfs/sysfs_class.c
@@ -509,16 +509,16 @@ static int get_classdev_path(const char *classname, const char *clsdev,
}
if (strncmp(classname, SYSFS_BLOCK_NAME,
sizeof(SYSFS_BLOCK_NAME)) == 0) {
- safestrncat(path, "/", len);
- safestrncat(path, SYSFS_BLOCK_NAME, len);
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, SYSFS_BLOCK_NAME, len);
} else {
- safestrncat(path, "/", len);
- safestrncat(path, SYSFS_CLASS_NAME, len);
- safestrncat(path, "/", len);
- safestrncat(path, classname, len);
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, SYSFS_CLASS_NAME, len);
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, classname, len);
}
- safestrncat(path, "/", len);
- safestrncat(path, clsdev, len);
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, clsdev, len);
return 0;
}
diff --git a/libsysfs/sysfs_device.c b/libsysfs/sysfs_device.c
index e3a8977016..290fd9723e 100644
--- a/libsysfs/sysfs_device.c
+++ b/libsysfs/sysfs_device.c
@@ -53,7 +53,7 @@ static int get_dev_driver(struct sysfs_device *dev)
if (c == NULL)
return 1;
*c = '\0';
- safestrncat(c, path, (sizeof(devpath) - strlen(devpath)));
+ safestrcatmax(c, path, (sizeof(devpath) - strlen(devpath)));
drvlist = sysfs_open_subsystem_list(path);
if (drvlist != NULL) {
diff --git a/libsysfs/sysfs_dir.c b/libsysfs/sysfs_dir.c
index be54a1cf86..8ce8522200 100644
--- a/libsysfs/sysfs_dir.c
+++ b/libsysfs/sysfs_dir.c
@@ -239,10 +239,10 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr,
sysattr->value = (char *)realloc
(sysattr->value, length);
sysattr->len = length;
- safestrncpy(sysattr->value, new_value, length);
+ safestrcpymax(sysattr->value, new_value, length);
} else {
/*"length" of the new value is same as old one */
- safestrncpy(sysattr->value, new_value, length);
+ safestrcpymax(sysattr->value, new_value, length);
}
}
@@ -348,7 +348,7 @@ int sysfs_read_attribute_value(const char *attrpath,
if (length > vsize)
dprintf("Value length %d is larger than supplied buffer %d\n",
length, vsize);
- safestrncpy(value, attr->value, vsize);
+ safestrcpymax(value, attr->value, vsize);
sysfs_close_attribute(attr);
return 0;
diff --git a/libsysfs/sysfs_driver.c b/libsysfs/sysfs_driver.c
index 9ffa9c9077..2439b7a567 100644
--- a/libsysfs/sysfs_driver.c
+++ b/libsysfs/sysfs_driver.c
@@ -335,14 +335,14 @@ static int get_driver_path(const char *bus, const char *drv,
dprintf("Error getting sysfs mount path\n");
return -1;
}
- safestrncat(path, "/", psize);
- safestrncat(path, SYSFS_BUS_NAME, psize);
- safestrncat(path, "/", psize);
- safestrncat(path, bus, psize);
- safestrncat(path, "/", psize);
- safestrncat(path, SYSFS_DRIVERS_NAME, psize);
- safestrncat(path, "/", psize);
- safestrncat(path, drv, psize);
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, SYSFS_BUS_NAME, psize);
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, bus, psize);
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, SYSFS_DRIVERS_NAME, psize);
+ safestrcatmax(path, "/", psize);
+ safestrcatmax(path, drv, psize);
return 0;
}
diff --git a/libsysfs/sysfs_utils.c b/libsysfs/sysfs_utils.c
index 699a9829b8..492c7fa668 100644
--- a/libsysfs/sysfs_utils.c
+++ b/libsysfs/sysfs_utils.c
@@ -67,7 +67,7 @@ static int sysfs_get_fs_mnt_path(const char *fs_type,
char *mnt_path, size_t len)
{
#ifdef __KLIBC__
- safestrncpy(mnt_path, "/sys", len);
+ safestrcpymax(mnt_path, "/sys", len);
return 0;
#else
FILE *mnt;
@@ -89,7 +89,7 @@ static int sysfs_get_fs_mnt_path(const char *fs_type,
if (strcmp(mntent->mnt_type, fs_type) == 0) {
dirlen = strlen(mntent->mnt_dir);
if (dirlen <= (len - 1)) {
- safestrncpy(mnt_path, mntent->mnt_dir, len);
+ safestrcpymax(mnt_path, mntent->mnt_dir, len);
} else {
dprintf("Error - mount path too long\n");
ret = -1;
@@ -126,7 +126,7 @@ int sysfs_get_mnt_path(char *mnt_path, size_t len)
}
sysfs_path = getenv(SYSFS_PATH_ENV);
if (sysfs_path != NULL) {
- safestrncpy(mnt_path, sysfs_path, len);
+ safestrcpymax(mnt_path, sysfs_path, len);
if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
return 1;
} else
@@ -166,7 +166,7 @@ int sysfs_get_name_from_path(const char *path, char *name, size_t len)
}
}
n++;
- safestrncpy(name, n, len);
+ safestrcpymax(name, n, len);
return 0;
}
@@ -221,7 +221,7 @@ int sysfs_get_link(const char *path, char *target, size_t len)
} else {
safestrcpy(temp_path, d);
}
- safestrncpy(target, temp_path, len);
+ safestrcpymax(target, temp_path, len);
break;
/*
* relative path
@@ -240,12 +240,12 @@ parse_path:
if (*s == '/')
count++;
}
- safestrncpy(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
- safestrncpy(target, devdir, len);
+ safestrcpymax(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
+ safestrcpymax(target, devdir, len);
break;
case '/':
/* absolute path - copy as is */
- safestrncpy(target, linkpath, len);
+ safestrcpymax(target, linkpath, len);
break;
default:
/* relative path from this directory */
@@ -257,7 +257,7 @@ parse_path:
} else {
safestrcpy(temp_path, linkpath);
}
- safestrncpy(target, temp_path, len);
+ safestrcpymax(target, temp_path, len);
}
return 0;
}
@@ -330,7 +330,7 @@ struct dlist *sysfs_open_subsystem_list(char *name)
dlist_for_each_data(dir->subdirs, cur,
struct sysfs_directory) {
subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
- safestrncpy(subsys_name, cur->name, SYSFS_NAME_LEN);
+ safestrcpymax(subsys_name, cur->name, SYSFS_NAME_LEN);
dlist_unshift_sorted(list, subsys_name, sort_char);
}
}
@@ -345,11 +345,11 @@ struct dlist *sysfs_open_subsystem_list(char *name)
if (c == NULL)
goto out;
*c = '\0';
- safestrncpy(c, SYSFS_BLOCK_NAME,
+ safestrcpymax(c, SYSFS_BLOCK_NAME,
sizeof(sysfs_path) - strlen(sysfs_path));
if ((sysfs_path_is_dir(sysfs_path)) == 0) {
subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
- safestrncpy(subsys_name, SYSFS_BLOCK_NAME,
+ safestrcpymax(subsys_name, SYSFS_BLOCK_NAME,
SYSFS_NAME_LEN);
dlist_unshift_sorted(list, subsys_name, sort_char);
}
@@ -409,7 +409,7 @@ struct dlist *sysfs_open_bus_devices_list(char *name)
dlist_for_each_data(dir->links, cur,
struct sysfs_link) {
device_name = (char *)calloc(1, SYSFS_NAME_LEN);
- safestrncpy(device_name, cur->name, SYSFS_NAME_LEN);
+ safestrcpymax(device_name, cur->name, SYSFS_NAME_LEN);
dlist_unshift_sorted(list, device_name, sort_char);
}
}