Changeset 2b5bd836 in rtems for cpukit/libblock


Ignore:
Timestamp:
Aug 25, 2010, 8:26:21 AM (9 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
6ef96da
Parents:
e0b6d43e
Message:

2010-08-25 Sebastian Huber <sebastian.huber@…>

CID 113/SECURE_CODING

  • libblock/src/media-path.c: Avoid strcpy(). Style. Use assert() to avoid dead code.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libblock/src/media-path.c

    re0b6d43e r2b5bd836  
    2525#include <string.h>
    2626#include <inttypes.h>
     27#include <assert.h>
    2728
    2829#include <rtems/media.h>
    2930
    30 char *rtems_media_create_path(const char *prefix, const char *name, rtems_device_major_number major)
     31char *rtems_media_create_path(
     32  const char *prefix,
     33  const char *name,
     34  rtems_device_major_number major
     35)
    3136{
    32   size_t const len = strlen(prefix) + 1 + strlen(name) + 1 + 10 + 1;
    33   char *const s = malloc(len);
     37  size_t const size = strlen(prefix) + 1 + strlen(name) + 1 + 10 + 1;
     38  char *const s = malloc(size);
    3439
    3540  if (s != NULL) {
    36     int rv = snprintf(s, len, "%s/%s-%" PRIu32, prefix, name, major);
    37 
    38     if (rv >= (int) len) {
    39       free(s);
    40 
    41       return NULL;
    42     }
     41    int rv = snprintf(s, size, "%s/%s-%" PRIu32, prefix, name, major);
     42    assert(rv < (int) size);
    4343  }
    4444
     
    5151  const char *const name = (name_try == NULL) ? path : name_try + 1;
    5252  size_t const new_prefix_len = strlen(new_prefix);
    53   size_t const name_len = strlen(name);
    54   size_t const len = new_prefix_len + 1 + name_len + 1;
    55   char *const s =  malloc(len);
     53  size_t const name_size = strlen(name) + 1;
     54  size_t const size = new_prefix_len + 1 + name_size;
     55  char *const s = malloc(size);
    5656
    5757  if (s != NULL) {
    58     strcpy(s, new_prefix);
    59     strcpy(s + new_prefix_len, "/");
    60     strcpy(s + new_prefix_len + 1, name);
     58    memcpy(s, new_prefix, new_prefix_len);
     59    s [new_prefix_len] = '/';
     60    memcpy(s + new_prefix_len + 1, name, name_size);
    6161  }
    6262
     
    6464}
    6565
    66 char *rtems_media_append_minor(const char *path, rtems_device_minor_number minor)
     66char *rtems_media_append_minor(
     67  const char *path,
     68  rtems_device_minor_number minor
     69)
    6770{
    68   size_t const len = strlen(path) + 1 + 10 + 1;
    69   char *const s = malloc(len);
     71  size_t const size = strlen(path) + 1 + 10 + 1;
     72  char *const s = malloc(size);
    7073
    7174  if (s != NULL) {
    72     int rv = snprintf(s, len, "%s-%" PRIu32, path, minor);
    73 
    74     if (rv >= (int) len) {
    75       free(s);
    76 
    77       return NULL;
    78     }
     75    int rv = snprintf(s, size, "%s-%" PRIu32, path, minor);
     76    assert(rv < (int) size);
    7977  }
    8078
Note: See TracChangeset for help on using the changeset viewer.