diff options
author | ananth@in.ibm.com <ananth@in.ibm.com> | 2004-03-12 00:57:36 -0800 |
---|---|---|
committer | Greg KH <gregkh@suse.de> | 2005-04-26 21:35:09 -0700 |
commit | 656703759d7d3eac6e8c86f1121cde7dfd6d8cbd (patch) | |
tree | 0f8161838607f73afdd30c3c7d3d3256d53ea5ec /libsysfs/sysfs_class.c | |
parent | d1fb871d99db38c7704d8e583ff5e0a00e713837 (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/sysfs_class.c')
-rw-r--r-- | libsysfs/sysfs_class.c | 16 |
1 files changed, 8 insertions, 8 deletions
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; } |