Message ID | 20240709213935.177028-22-mwilck@suse.com (mailing list archive) |
---|---|
State | Not Applicable, archived |
Delegated to: | christophe varoqui |
Headers | show |
Series | multipath-tools: devmapper API refactored | expand |
On Tue, Jul 09, 2024 at 11:39:12PM +0200, Martin Wilck wrote: > Use sscanf to make the parsing of the UUID more robust. > > Signed-off-by: Martin Wilck <mwilck@suse.com> > --- > libmultipath/devmapper.c | 17 +++++++---------- > 1 file changed, 7 insertions(+), 10 deletions(-) > > diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c > index 56157af..d62a7dd 100644 > --- a/libmultipath/devmapper.c > +++ b/libmultipath/devmapper.c > @@ -846,23 +846,20 @@ int dm_get_uuid(const char *name, char *uuid, int uuid_len) > > static int is_mpath_part(const char *part_name, const char *map_name) > { > - char *p; > - char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN]; > + char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN], c; > + int np, nc; > > if (dm_get_dm_uuid(part_name, part_uuid) != DMP_OK) > return 0; > > + if (2 != sscanf(part_uuid, "part%d-%n" UUID_PREFIX "%c", &np, &nc, &c) we should probably use "part%u-%n" so we can't match a "-" before the number. -Ben > + || np <= 0) > + return 0; > + > if (dm_get_dm_uuid(map_name, map_uuid) != DMP_OK) > return 0; > > - if (strncmp(part_uuid, "part", 4) != 0) > - return 0; > - > - p = strstr(part_uuid, UUID_PREFIX); > - if (p && !strcmp(p, map_uuid)) > - return 1; > - > - return 0; > + return !strcmp(part_uuid + nc, map_uuid); > } > > int dm_get_status(const char *name, char **outstatus) > -- > 2.45.2
On Wed, 2024-07-10 at 18:54 -0400, Benjamin Marzinski wrote: > On Tue, Jul 09, 2024 at 11:39:12PM +0200, Martin Wilck wrote: > > Use sscanf to make the parsing of the UUID more robust. > > > > Signed-off-by: Martin Wilck <mwilck@suse.com> > > --- > > libmultipath/devmapper.c | 17 +++++++---------- > > 1 file changed, 7 insertions(+), 10 deletions(-) > > > > diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c > > index 56157af..d62a7dd 100644 > > --- a/libmultipath/devmapper.c > > +++ b/libmultipath/devmapper.c > > @@ -846,23 +846,20 @@ int dm_get_uuid(const char *name, char *uuid, > > int uuid_len) > > > > static int is_mpath_part(const char *part_name, const char > > *map_name) > > { > > - char *p; > > - char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN]; > > + char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN], c; > > + int np, nc; > > > > if (dm_get_dm_uuid(part_name, part_uuid) != DMP_OK) > > return 0; > > > > + if (2 != sscanf(part_uuid, "part%d-%n" UUID_PREFIX "%c", > > &np, &nc, &c) > > we should probably use "part%u-%n" so we can't match a "-" before the > number. That doesn't work, %u accepts negative numbers, too (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) That's why I use int and check whether the result is positive. Martin
On Thu, Jul 11, 2024 at 01:08:39PM +0200, Martin Wilck wrote: > On Wed, 2024-07-10 at 18:54 -0400, Benjamin Marzinski wrote: > > On Tue, Jul 09, 2024 at 11:39:12PM +0200, Martin Wilck wrote: > > > Use sscanf to make the parsing of the UUID more robust. > > > > > > Signed-off-by: Martin Wilck <mwilck@suse.com> > > > --- > > > libmultipath/devmapper.c | 17 +++++++---------- > > > 1 file changed, 7 insertions(+), 10 deletions(-) > > > > > > diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c > > > index 56157af..d62a7dd 100644 > > > --- a/libmultipath/devmapper.c > > > +++ b/libmultipath/devmapper.c > > > @@ -846,23 +846,20 @@ int dm_get_uuid(const char *name, char *uuid, > > > int uuid_len) > > > > > > static int is_mpath_part(const char *part_name, const char > > > *map_name) > > > { > > > - char *p; > > > - char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN]; > > > + char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN], c; > > > + int np, nc; > > > > > > if (dm_get_dm_uuid(part_name, part_uuid) != DMP_OK) > > > return 0; > > > > > > + if (2 != sscanf(part_uuid, "part%d-%n" UUID_PREFIX "%c", > > > &np, &nc, &c) > > > > we should probably use "part%u-%n" so we can't match a "-" before the > > number. > > That doesn't work, %u accepts negative numbers, too > (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) > That's why I use int and check whether the result is positive. Huh? I learned something new. I actually find that kind of annoying, since you could get the same result by just using %d and casting a unsigned int to a signed one for the assignment, and it seems like people would often not want to match a dash at the start. At any rate, you're correct. Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com> > > Martin
On Thu, 2024-07-11 at 13:01 -0400, Benjamin Marzinski wrote: > On Thu, Jul 11, 2024 at 01:08:39PM +0200, Martin Wilck wrote: > > On Wed, 2024-07-10 at 18:54 -0400, Benjamin Marzinski wrote: > > > > > > we should probably use "part%u-%n" so we can't match a "-" before > > > the > > > number. > > > > That doesn't work, %u accepts negative numbers, too > > (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) > > That's why I use int and check whether the result is positive. > > Huh? I learned something new. I actually find that kind of annoying, I know, I was also surprised when I found out. But strtoul() behaves like this, too. In C, it's perfectly ok to write unsigned int n = -1; Maybe that's how we should look at it. Martin
diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c index 56157af..d62a7dd 100644 --- a/libmultipath/devmapper.c +++ b/libmultipath/devmapper.c @@ -846,23 +846,20 @@ int dm_get_uuid(const char *name, char *uuid, int uuid_len) static int is_mpath_part(const char *part_name, const char *map_name) { - char *p; - char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN]; + char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN], c; + int np, nc; if (dm_get_dm_uuid(part_name, part_uuid) != DMP_OK) return 0; + if (2 != sscanf(part_uuid, "part%d-%n" UUID_PREFIX "%c", &np, &nc, &c) + || np <= 0) + return 0; + if (dm_get_dm_uuid(map_name, map_uuid) != DMP_OK) return 0; - if (strncmp(part_uuid, "part", 4) != 0) - return 0; - - p = strstr(part_uuid, UUID_PREFIX); - if (p && !strcmp(p, map_uuid)) - return 1; - - return 0; + return !strcmp(part_uuid + nc, map_uuid); } int dm_get_status(const char *name, char **outstatus)
Use sscanf to make the parsing of the UUID more robust. Signed-off-by: Martin Wilck <mwilck@suse.com> --- libmultipath/devmapper.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)