Skip to contentSkip to conversation

rangeset: no need to use snprintf()

PATCH In review Jan Beulich 2 participants 3 messages Apr 6, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rangeset: no need to use snprintf()

As of the conversion to safe_strcpy() years ago there has been no need anymore to use snprintf() to prevent storing a not-nul-terminated string.

xen/common/rangeset.c +1 -8
@@ -436,14 +436,7 @@ struct rangeset *rangeset_new(
     BUG_ON(flags & ~RANGESETF_prettyprint_hex);
     r->flags = flags;
 
-    if ( name != NULL )
-    {
-        safe_strcpy(r->name, name);
-    }
-    else
-    {
-        snprintf(r->name, sizeof(r->name), "(no name)");
-    }
+    safe_strcpy(r->name, name ?: "(no name)");
 
     if ( (r->domain = d) != NULL )
     {
Acked-by on v1 1/1
#1 lore raw

Hi Jan,

Quoted 4 lines · Jan Beulich

As of the conversion to safe_strcpy() years ago there has been no need anymore to use snprintf() to prevent storing a not-nul-terminated string.

Quoted 6 lines
xen/common/rangeset.c +0 -0
@@ -436,14 +436,7 @@ struct rangeset *rangeset_new(
      BUG_ON(flags & ~RANGESETF_prettyprint_hex);
      r->flags = flags;

xen/common/rangeset.c struct rangeset *rangeset_new( · line 439

Quoted 10 lines
xen/common/rangeset.c +1 -8
 
-    if ( name != NULL )
-    {
-        safe_strcpy(r->name, name);
-    }
-    else
-    {
-        snprintf(r->name, sizeof(r->name), "(no name)");
-    }
+    safe_strcpy(r->name, name ?: "(no name)");

I realize the current code is not checking the return, but I wonder we should rather than silently truncating the string.

This is not a new issue, so it can dealt separately if we decide to check the return.

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 7 lines · Julien Grall
Quoted 4 lines · Jan Beulich

As of the conversion to safe_strcpy() years ago there has been no need anymore to use snprintf() to prevent storing a not-nul-terminated string.

Thanks.

Quoted 6 lines
Quoted 6 lines
xen/common/rangeset.c +0 -0
@@ -436,14 +436,7 @@ struct rangeset *rangeset_new(
      BUG_ON(flags & ~RANGESETF_prettyprint_hex);
      r->flags = flags;
 
Quoted 12 lines

xen/common/rangeset.c struct rangeset *rangeset_new( · line 439

Quoted 9 lines
xen/common/rangeset.c +1 -8
-    if ( name != NULL )
-    {
-        safe_strcpy(r->name, name);
-    }
-    else
-    {
-        snprintf(r->name, sizeof(r->name), "(no name)");
-    }
+    safe_strcpy(r->name, name ?: "(no name)");

I realize the current code is not checking the return, but I wonder we should rather than silently truncating the string.

The name field is used only for display purposes, so I guess truncation wouldn't really be a problem here.

Jan