Re: [PATCH v2] rtems-fdt / shell - Fix string truncation warning

2020-10-19 Thread Chris Johns
OK to push.

Thanks
Chris

On 20/10/20 12:16 am, Frank Kuehndel wrote:
> From: Frank Kühndel 
> 
> The compiler warning was:
> 
> ../../../cpukit/libmisc/rtems-fdt/rtems-fdt.c:267:5: warning:
> 'strncpy' specified bound depends on the length of the source argument
>   267 | strncpy(path, name, namelen);
>   | ^~~~
> 
> It turns out that the `strncpy()` nor the buffer `path` is needed when
> one uses `strncmp()` instead of `strcmp()`. This needs some change to
> the algorithm but has the advantage that `name` is never truncated
> to the size of the buffer `path`.
> ---
>  cpukit/libmisc/rtems-fdt/rtems-fdt.c | 40 +---
>  1 file changed, 19 insertions(+), 21 deletions(-)
> 
> diff --git a/cpukit/libmisc/rtems-fdt/rtems-fdt.c 
> b/cpukit/libmisc/rtems-fdt/rtems-fdt.c
> index 39e70bffec..0ea365314f 100644
> --- a/cpukit/libmisc/rtems-fdt/rtems-fdt.c
> +++ b/cpukit/libmisc/rtems-fdt/rtems-fdt.c
> @@ -248,48 +248,46 @@ rtems_fdt_index_find_by_name(rtems_fdt_index* index,
>  {
>int min = 0;
>int max = index->num_entries;
> -  charpath[256];
> -  const char* cmp_name = name;
> -
>/*
> * Handle trailing slash case.
> */
> -  int namelen = strlen(name);
> +  size_t namelen = strlen(name);
>if (namelen > 0 && name[namelen-1] == '/')
>{
>  namelen--;
> -
> -if (namelen >= (int)sizeof(path) - 1)
> -{
> -  namelen = sizeof(path) - 1;
> -}
> -
> -strncpy(path, name, namelen);
> -path[namelen] = 0;
> -cmp_name = path;
>}
>  
>/* Binary search for the name. */
>while (min < max)
>{
>  int middle = (min + max) / 2;
> -int cmp = strcmp(cmp_name, index->entries[middle].name);
> +int cmp = strncmp(name, index->entries[middle].name, namelen);
> +if (cmp == 0)
> +{
> +  /* 'namelen' characters are equal but 'index->entries[middle].name' */
> +  /* could have additional characters. */
> +  if (index->entries[middle].name[namelen] == '\0')
> +  {
> +/* Found it. */
> +return index->entries[middle].offset;
> +  }
> +  else
> +  {
> + /* 'index->entries[middle].name' is longer than 'name'. */
> + cmp = -1;
> +  }
> +}
>  if (cmp < 0)
>  {
>/* Look lower than here. */
>max = middle;
>  }
> -else if (cmp > 0)
> +else
>  {
>/* Look higher than here. */
>min = middle + 1;
>  }
> -else
> -{
> -  /* Found it. */
> -  return index->entries[middle].offset;
> -}
> -  }
> + }
>  
>/* Didn't find it. */
>return -FDT_ERR_NOTFOUND;
> 
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

[PATCH v2] rtems-fdt / shell - Fix string truncation warning

2020-10-19 Thread Frank Kuehndel
From: Frank Kühndel 

The compiler warning was:

../../../cpukit/libmisc/rtems-fdt/rtems-fdt.c:267:5: warning:
'strncpy' specified bound depends on the length of the source argument
  267 | strncpy(path, name, namelen);
  | ^~~~

It turns out that the `strncpy()` nor the buffer `path` is needed when
one uses `strncmp()` instead of `strcmp()`. This needs some change to
the algorithm but has the advantage that `name` is never truncated
to the size of the buffer `path`.
---
 cpukit/libmisc/rtems-fdt/rtems-fdt.c | 40 +---
 1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/cpukit/libmisc/rtems-fdt/rtems-fdt.c 
b/cpukit/libmisc/rtems-fdt/rtems-fdt.c
index 39e70bffec..0ea365314f 100644
--- a/cpukit/libmisc/rtems-fdt/rtems-fdt.c
+++ b/cpukit/libmisc/rtems-fdt/rtems-fdt.c
@@ -248,48 +248,46 @@ rtems_fdt_index_find_by_name(rtems_fdt_index* index,
 {
   int min = 0;
   int max = index->num_entries;
-  charpath[256];
-  const char* cmp_name = name;
-
   /*
* Handle trailing slash case.
*/
-  int namelen = strlen(name);
+  size_t namelen = strlen(name);
   if (namelen > 0 && name[namelen-1] == '/')
   {
 namelen--;
-
-if (namelen >= (int)sizeof(path) - 1)
-{
-  namelen = sizeof(path) - 1;
-}
-
-strncpy(path, name, namelen);
-path[namelen] = 0;
-cmp_name = path;
   }
 
   /* Binary search for the name. */
   while (min < max)
   {
 int middle = (min + max) / 2;
-int cmp = strcmp(cmp_name, index->entries[middle].name);
+int cmp = strncmp(name, index->entries[middle].name, namelen);
+if (cmp == 0)
+{
+  /* 'namelen' characters are equal but 'index->entries[middle].name' */
+  /* could have additional characters. */
+  if (index->entries[middle].name[namelen] == '\0')
+  {
+/* Found it. */
+return index->entries[middle].offset;
+  }
+  else
+  {
+ /* 'index->entries[middle].name' is longer than 'name'. */
+ cmp = -1;
+  }
+}
 if (cmp < 0)
 {
   /* Look lower than here. */
   max = middle;
 }
-else if (cmp > 0)
+else
 {
   /* Look higher than here. */
   min = middle + 1;
 }
-else
-{
-  /* Found it. */
-  return index->entries[middle].offset;
-}
-  }
+ }
 
   /* Didn't find it. */
   return -FDT_ERR_NOTFOUND;
-- 
2.26.2

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel