Skip to content

Commit 675d858

Browse files
nvinsonDaniel Kiper
authored andcommitted
osdep/linux/ofpath: Update strstr() calls
With C23, strstr() returns a "const char *" if its first argument is "const char *", and these changes are implemented in glibc-2.43. As a result, the first strstr() call in check_sas() now returns a "const char *" instead of "char *" because sysfs_path is a "const char *". This triggers a "discards qualifiers" warning, that is later promoted to an error and ultimately causes the build to fail. To fix the issue, this patch converts "ed", the pointer that stores strstr()'s return value, to a "const char *". Removes the xstrdup() call and cleans up the rest of the function by updating the *printf() calls and using pointer arithmetic to compute lengths instead of strlen(). Signed-off-by: Nicholas Vinson <nvinson234@gmail.com> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
1 parent 170221b commit 675d858

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

grub-core/osdep/linux/ofpath.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,11 @@ check_hba_identifiers (const char *sysfs_path, int *vendor, int *device_id)
488488
static void
489489
check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
490490
{
491-
char *ed = strstr (sysfs_path, "end_device");
492-
char *p, *q, *path;
491+
const char *ed = strstr (sysfs_path, "end_device");
492+
int p_len;
493+
int ed_len;
494+
const char *q;
495+
char *path;
493496
char phy[21];
494497
int fd;
495498
size_t path_size;
@@ -498,20 +501,16 @@ check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
498501
return;
499502

500503
/* SAS devices are identified using disk@$PHY_ID */
501-
p = xstrdup (sysfs_path);
502-
ed = strstr(p, "end_device");
503-
if (!ed)
504-
return;
505-
506504
q = ed;
507505
while (*q && *q != '/')
508506
q++;
509-
*q = '\0';
507+
p_len = (int) (q - sysfs_path);
508+
ed_len = (int) (q - ed);
510509

511-
path_size = (strlen (p) + strlen (ed)
512-
+ sizeof ("%s/sas_device/%s/phy_identifier"));
510+
path_size = (p_len + ed_len + sizeof ("%s/sas_device/%s/phy_identifier"));
513511
path = xmalloc (path_size);
514-
snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
512+
snprintf (path, path_size, "%.*s/sas_device/%.*s/phy_identifier", p_len,
513+
sysfs_path, ed_len, ed);
515514
fd = open (path, O_RDONLY);
516515
if (fd < 0)
517516
grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
@@ -524,7 +523,8 @@ check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
524523

525524
sscanf (phy, "%d", tgt);
526525

527-
snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed);
526+
snprintf (path, path_size, "%.*s/sas_device/%.*s/sas_address", p_len,
527+
sysfs_path, ed_len, ed);
528528
fd = open (path, O_RDONLY);
529529
if (fd < 0)
530530
grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
@@ -535,7 +535,6 @@ check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
535535
sscanf (phy, "%lx", sas_address);
536536

537537
free (path);
538-
free (p);
539538
close (fd);
540539
}
541540

0 commit comments

Comments
 (0)