Skip to content

Commit

Permalink
libidset: add idset_ndecode()
Browse files Browse the repository at this point in the history
Problem: there is no convenient way to decode an idset
from a bounded substring.

Add idset_ndecode() which accepts a length.
  • Loading branch information
garlick committed Dec 19, 2019
1 parent 602aa4f commit 193859f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/common/libidset/idset.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ char *idset_encode (const struct idset *idset, int flags);
*/
struct idset *idset_decode (const char *s);

/* Decode 'len' chars of string 's' to an idset.
* Returns idset on success, or NULL on failure with errno set.
*/
struct idset *idset_ndecode (const char *s, size_t len);

/* Add id (or range [lo-hi]) to idset.
* Return 0 on success, -1 on failure with errno set.
*/
Expand Down
9 changes: 7 additions & 2 deletions src/common/libidset/idset_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static char *trim_brackets (char *s)
return p;
}

struct idset *idset_decode (const char *str)
struct idset *idset_ndecode (const char *str, size_t size)
{
struct idset *idset;
char *cpy = NULL;
Expand All @@ -74,7 +74,7 @@ struct idset *idset_decode (const char *str)
}
if (!(idset = idset_create (0, IDSET_FLAG_AUTOGROW)))
return NULL;
if (!(cpy = strdup (str)))
if (!(cpy = strndup (str, size)))
goto error;
a1 = trim_brackets (cpy);
saveptr = NULL;
Expand Down Expand Up @@ -104,6 +104,11 @@ struct idset *idset_decode (const char *str)
return NULL;
}

struct idset *idset_decode (const char *str)
{
return idset_ndecode (str, str ? strlen (str) : 0);
}

/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/

0 comments on commit 193859f

Please sign in to comment.