Changeset daeb0a1 in rtems for cpukit/libmisc


Ignore:
Timestamp:
Oct 19, 2020, 12:54:39 PM (6 weeks ago)
Author:
Frank Kühndel <frank.kuehndel@…>
Branches:
master
Children:
3fccdc95
Parents:
bb4e534
git-author:
Frank Kühndel <frank.kuehndel@…> (10/19/20 12:54:39)
git-committer:
Sebastian Huber <sebastian.huber@…> (10/20/20 05:01:29)
Message:

rtems-fdt / shell - Fix string truncation warning

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libmisc/rtems-fdt/rtems-fdt.c

    rbb4e534 rdaeb0a1  
    249249  int         min = 0;
    250250  int         max = index->num_entries;
    251   char        path[256];
    252   const char* cmp_name = name;
    253 
    254251  /*
    255252   * Handle trailing slash case.
    256253   */
    257   int namelen = strlen(name);
     254  size_t namelen = strlen(name);
    258255  if (namelen > 0 && name[namelen-1] == '/')
    259256  {
    260257    namelen--;
    261 
    262     if (namelen >= (int)sizeof(path) - 1)
    263     {
    264       namelen = sizeof(path) - 1;
    265     }
    266 
    267     strncpy(path, name, namelen);
    268     path[namelen] = 0;
    269     cmp_name = path;
    270258  }
    271259
     
    274262  {
    275263    int middle = (min + max) / 2;
    276     int cmp = strcmp(cmp_name, index->entries[middle].name);
     264    int cmp = strncmp(name, index->entries[middle].name, namelen);
     265    if (cmp == 0)
     266    {
     267      /* 'namelen' characters are equal but 'index->entries[middle].name' */
     268      /* could have additional characters. */
     269      if (index->entries[middle].name[namelen] == '\0')
     270      {
     271        /* Found it. */
     272        return index->entries[middle].offset;
     273      }
     274      else
     275      {
     276         /* 'index->entries[middle].name' is longer than 'name'. */
     277         cmp = -1;
     278      }
     279    }
    277280    if (cmp < 0)
    278281    {
     
    280283      max = middle;
    281284    }
    282     else if (cmp > 0)
     285    else
    283286    {
    284287      /* Look higher than here. */
    285288      min = middle + 1;
    286289    }
    287     else
    288     {
    289       /* Found it. */
    290       return index->entries[middle].offset;
    291     }
    292   }
     290 }
    293291
    294292  /* Didn't find it. */
Note: See TracChangeset for help on using the changeset viewer.