diff mbox series

[1/8] core/pci: pci_slot_add_loc use NUL terminated strings

Message ID 20190427144459.6719-2-npiggin@gmail.com
State Superseded
Headers show
Series reduce instructions required to boot | expand

Commit Message

Nicholas Piggin April 27, 2019, 2:44 p.m. UTC
Use NUL terminated strings consistently, making the maximum string
length in all cases the same, and avoiding dt_add_property_nstr.

This avoids the following warning that appears after adding more
checking to string ops:

  core/pci-slot.c: In function ‘pci_slot_add_loc’:
  skiboot/libc/include/string.h:19:17: warning: ‘__builtin_strncpy’
    specified bound 80 equals destination size [-Wstringop-truncation]
   #define strncpy __builtin_strncpy
  core/pci-slot.c:244:3: note: in expansion of macro ‘strncpy’
     strncpy(loc_code, label, sizeof(loc_code));
     ^~~~~~~

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 core/pci-slot.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Vasant Hegde May 7, 2019, 6:37 a.m. UTC | #1
On 04/27/2019 08:14 PM, Nicholas Piggin wrote:
> Use NUL terminated strings consistently, making the maximum string
> length in all cases the same, and avoiding dt_add_property_nstr.
> 
> This avoids the following warning that appears after adding more
> checking to string ops:
> 
>    core/pci-slot.c: In function ‘pci_slot_add_loc’:
>    skiboot/libc/include/string.h:19:17: warning: ‘__builtin_strncpy’
>      specified bound 80 equals destination size [-Wstringop-truncation]
>     #define strncpy __builtin_strncpy
>    core/pci-slot.c:244:3: note: in expansion of macro ‘strncpy’
>       strncpy(loc_code, label, sizeof(loc_code));
>       ^~~~~~~
> 
> Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
> ---
>   core/pci-slot.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/core/pci-slot.c b/core/pci-slot.c
> index 497d0a47f..54f25befe 100644
> --- a/core/pci-slot.c
> +++ b/core/pci-slot.c
> @@ -222,7 +222,7 @@ struct pci_slot *pci_slot_find(uint64_t id)
>   void pci_slot_add_loc(struct pci_slot *slot,
>   			struct dt_node *np, const char *label)
>   {
> -	char tmp[8], loc_code[LOC_CODE_SIZE];
> +	char tmp[8], loc_code[LOC_CODE_SIZE + 1];

Looks like you included this patch in other series as well?

As mentioned in other series, per spec, location code size is 80 char including 
NULL character.

-Vasant
diff mbox series

Patch

diff --git a/core/pci-slot.c b/core/pci-slot.c
index 497d0a47f..54f25befe 100644
--- a/core/pci-slot.c
+++ b/core/pci-slot.c
@@ -222,7 +222,7 @@  struct pci_slot *pci_slot_find(uint64_t id)
 void pci_slot_add_loc(struct pci_slot *slot,
 			struct dt_node *np, const char *label)
 {
-	char tmp[8], loc_code[LOC_CODE_SIZE];
+	char tmp[8], loc_code[LOC_CODE_SIZE + 1];
 	struct pci_device *pd = slot->pd;
 	struct phb *phb = slot->phb;
 
@@ -241,10 +241,10 @@  void pci_slot_add_loc(struct pci_slot *slot,
 		snprintf(loc_code, sizeof(loc_code), "%s-%s",
 			phb->base_loc_code, label);
 	} else {
-		strncpy(loc_code, label, sizeof(loc_code));
+		strncpy(loc_code, label, sizeof(loc_code) - 1);
+		loc_code[LOC_CODE_SIZE] = '\0';
 	}
 
 	dt_add_property_string(np, "ibm,slot-label", label);
-	dt_add_property_nstr(np, "ibm,slot-location-code", loc_code,
-				sizeof(loc_code));
+	dt_add_property_string(np, "ibm,slot-location-code", loc_code);
 }