[PATCH] Fix CppCheck errors

Gedare Bloom gedare at rtems.org
Wed Sep 2 13:35:32 UTC 2015


Looks OK to me.

On Tue, Sep 1, 2015 at 3:10 PM, Martin Galvan
<martin.galvan at tallertechnologies.com> wrote:
> This patch fixes the following CppCheck errors found throughout the code:
>
> [c/src/lib/libbsp/shared/umon/umon.h:21]: (error) Invalid number of character ({) when these macros are defined: '__cplusplus'.
> [cpukit/libmisc/dumpbuf/dumpbuf.c:69]: (error) Undefined behavior: Variable 'line_buffer' is used as parameter and destination in s[n]printf().
> [cpukit/libmisc/dumpbuf/dumpbuf.c:76]: (error) Undefined behavior: Variable 'line_buffer' is used as parameter and destination in s[n]printf().
> [cpukit/libnetworking/rtems/rtems_dhcp.c:401]: (error) Common realloc mistake: 'dhcp_hostname' nulled but not freed upon failure
> [cpukit/posix/include/rtems/posix/ptimer.h:33]: (error) Invalid number of character ({) when these macros are defined: '__cplusplus'.
> [cpukit/rtems/include/rtems/rtems/dpmemimpl.h:104]: (error) Invalid number of character ({) when these macros are defined: '__cplusplus'.
> [tools/cpu/nios2/memory.c:99]: (error) Uninitialized variable: memory
> [tools/cpu/nios2/ptf.c:582]: (error) Memory leak: new_prefix
>
> Closes #2405.
>
> ---
>  c/src/lib/libbsp/shared/umon/umon.h          |  4 +++
>  cpukit/libmisc/dumpbuf/dumpbuf.c             | 41 ++++++++++++++++------------
>  cpukit/libnetworking/rtems/rtems_dhcp.c      | 18 ++++++++----
>  cpukit/posix/include/rtems/posix/ptimer.h    |  5 +++-
>  cpukit/rtems/include/rtems/rtems/dpmemimpl.h |  6 +++-
>  tools/cpu/nios2/memory.c                     |  5 ++--
>  tools/cpu/nios2/ptf.c                        | 24 +++++++++-------
>  7 files changed, 65 insertions(+), 38 deletions(-)
>
> diff --git a/c/src/lib/libbsp/shared/umon/umon.h b/c/src/lib/libbsp/shared/umon/umon.h
> index 3c5bdf9..d25a781 100644
> --- a/c/src/lib/libbsp/shared/umon/umon.h
> +++ b/c/src/lib/libbsp/shared/umon/umon.h
> @@ -61,4 +61,8 @@ int rtems_initialize_tfs_filesystem(
>   */
>  int umoncons_poll_read(int minor);
>
> +#ifdef __cplusplus
> +}
>  #endif
> +
> +#endif /* __rtems_umon_h */
> diff --git a/cpukit/libmisc/dumpbuf/dumpbuf.c b/cpukit/libmisc/dumpbuf/dumpbuf.c
> index 9d34d42..aca32f3 100644
> --- a/cpukit/libmisc/dumpbuf/dumpbuf.c
> +++ b/cpukit/libmisc/dumpbuf/dumpbuf.c
> @@ -6,7 +6,7 @@
>   */
>
>  /*
> - *  COPYRIGHT (c) 1997-2007.
> + *  COPYRIGHT (c) 1997-2015.
>   *  On-Line Applications Research Corporation (OAR).
>   *
>   *  The license and distribution terms for this file may in
> @@ -24,6 +24,9 @@
>  #include <rtems/dumpbuf.h>
>  #include <rtems/bspIo.h>
>
> +#define BUFFER_LENGTH 120
> +#define PADDING_LENGTH 16
> +
>  /*
>   *  Put the body below rtems_print_buffer so it won't get inlined.
>   */
> @@ -59,27 +62,29 @@ static inline void Dump_Line(
>    int                  length
>  )
>  {
> +  unsigned int i;
> +  static unsigned char line_buffer[BUFFER_LENGTH];
> +  static unsigned char tmp[BUFFER_LENGTH] = "";
>
> -  int  i;
> -  char line_buffer[120];
> -
> -  line_buffer[0] = '\0';
> -
> -  for( i=0 ; i<length ; i++ )
> -    sprintf( line_buffer, "%s%02x ", line_buffer, buffer[ i ] );
> +  for ( i = 0; i < length; ++i ) {
> +    sprintf(tmp, "%02x ", buffer[i]);
> +    strncat(line_buffer, tmp, strlen(tmp));
> +  }
>
> -  for( ; i<16 ; i++ )
> -    strcat( line_buffer, "   " );
> +  for ( ; i < PADDING_LENGTH; ++i ) {
> +    strcat(line_buffer, "   ");
> +  }
>
> -  strcat( line_buffer, "|" );
> -  for( i=0 ; i<length ; i++ )
> -    sprintf( line_buffer, "%s%c", line_buffer,
> -             isprint( buffer[ i ] ) ? buffer[ i ] : '.' );
> +  strcat(line_buffer, "|");
>
> -  for( ; i<16 ; i++ )
> -    strcat( line_buffer, " " );
> +  for ( i = 0; i < length; ++i ) {
> +    sprintf(tmp, "%c", isprint(buffer[i]) ? buffer[i] : '.');
> +    strncat(line_buffer, tmp, strlen(tmp));
> +  }
>
> -  strcat( line_buffer, "|\n" );
> +  for ( ; i < PADDING_LENGTH; ++i ) {
> +    strcat(line_buffer, " ");
> +  }
>
> -  printk( line_buffer );
> +  printk("%s|\n", line_buffer);
>  }
> diff --git a/cpukit/libnetworking/rtems/rtems_dhcp.c b/cpukit/libnetworking/rtems/rtems_dhcp.c
> index c938ee0..87be238 100644
> --- a/cpukit/libnetworking/rtems/rtems_dhcp.c
> +++ b/cpukit/libnetworking/rtems/rtems_dhcp.c
> @@ -394,15 +394,23 @@ process_options (unsigned char *optbuf, int optbufSize)
>            printf ("dhcpc: hostname >= %d bytes\n", MAXHOSTNAMELEN);
>            len = MAXHOSTNAMELEN-1;
>          }
> -        if (sethostname (p, len) < 0)
> +        if (sethostname (p, len) < 0) {
>            printf ("dhcpc: can't set host name");
> +        }
>          if (dhcp_hostname != NULL)
>          {
> -          dhcp_hostname = realloc (dhcp_hostname, len);
> -          strncpy (dhcp_hostname, p, len);
> -        }
> -        else
> +          char *tmp = realloc (dhcp_hostname, len);
> +          if (tmp != NULL) {
> +            dhcp_hostname = tmp;
> +            strncpy (dhcp_hostname, p, len);
> +          } else {  /* realloc failed */
> +            printf ("dhcpc: realloc failed (%s:%d)", __FILE__, __LINE__);
> +            free (dhcp_hostname);
> +            dhcp_hostname = NULL;
> +          }
> +        } else { /* dhcp_hostname == NULL */
>            dhcp_hostname = strndup (p, len);
> +        }
>          break;
>
>        case 7:
> diff --git a/cpukit/posix/include/rtems/posix/ptimer.h b/cpukit/posix/include/rtems/posix/ptimer.h
> index 7cc0516..16ac2b8 100644
> --- a/cpukit/posix/include/rtems/posix/ptimer.h
> +++ b/cpukit/posix/include/rtems/posix/ptimer.h
> @@ -87,6 +87,9 @@ int timer_getoverrun(
>    timer_t   timerid
>  );
>
> +#ifdef __cplusplus
> +}
> +#endif
>  /** @} */
>
> -#endif
> +#endif /* _RTEMS_POSIX_PTIMER_H */
> diff --git a/cpukit/rtems/include/rtems/rtems/dpmemimpl.h b/cpukit/rtems/include/rtems/rtems/dpmemimpl.h
> index 9fd2e6c..d2d7a4f 100644
> --- a/cpukit/rtems/include/rtems/rtems/dpmemimpl.h
> +++ b/cpukit/rtems/include/rtems/rtems/dpmemimpl.h
> @@ -20,6 +20,10 @@
>  #include <rtems/rtems/dpmem.h>
>  #include <rtems/score/objectimpl.h>
>
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
>  /**
>   * @defgroup ClassicDPMEMImpl Dual Ported Memory Manager Implementation
>   *
> @@ -104,5 +108,5 @@ RTEMS_INLINE_ROUTINE Dual_ported_memory_Control *_Dual_ported_memory_Get (
>  }
>  #endif
>
> -#endif
> +#endif /* _RTEMS_RTEMS_DPMEM_INL */
>  /* end of include file */
> diff --git a/tools/cpu/nios2/memory.c b/tools/cpu/nios2/memory.c
> index cd88b8b..cb1ea7f 100644
> --- a/tools/cpu/nios2/memory.c
> +++ b/tools/cpu/nios2/memory.c
> @@ -18,7 +18,8 @@ memory_desc *find_memory(device_desc *devices)
>  {
>    struct ptf *p;
>    struct ptf_item pi;
> -  memory_desc *tmd, *memory;
> +  memory_desc *tmd;
> +  memory_desc *memory = NULL;
>
>    /********************************************************/
>    /* Check which of the devices are memory, sort by size */
> @@ -29,8 +30,6 @@ memory_desc *find_memory(device_desc *devices)
>      struct ptf_item pi;
>      device_desc *dd;
>
> -    memory = NULL;
> -
>      for(dd = devices; dd; dd=dd->next)
>      {
>        p = ptf_find(dd->ptf->sub, &pi, item, "Is_Memory_Device", "1");
> diff --git a/tools/cpu/nios2/ptf.c b/tools/cpu/nios2/ptf.c
> index 7a31c11..07d6183 100644
> --- a/tools/cpu/nios2/ptf.c
> +++ b/tools/cpu/nios2/ptf.c
> @@ -567,17 +567,21 @@ void ptf_printf(FILE *s, struct ptf *tree, char *prefix)
>            new_prefix_len += strlen(leaf->value) + 1;
>          };
>          new_prefix = (char *)malloc(new_prefix_len);
> -        strcpy(new_prefix, prefix);
> -        strcat(new_prefix, leaf->name);
> -        if(leaf->value != NULL && leaf->value[0] != 0)
> +        if (new_prefix != NULL)
>          {
> -          strcat(new_prefix, ":");
> -          strcat(new_prefix, leaf->value);
> -        };
> -        strcat(new_prefix, "/");
> -        fputs(new_prefix, s);
> -        fputs("\n", s);
> -        ptf_printf(s, leaf->sub, new_prefix);
> +          strcpy(new_prefix, prefix);
> +          strcat(new_prefix, leaf->name);
> +          if(leaf->value != NULL && leaf->value[0] != 0)
> +          {
> +            strcat(new_prefix, ":");
> +            strcat(new_prefix, leaf->value);
> +          };
> +          strcat(new_prefix, "/");
> +          fputs(new_prefix, s);
> +          fputs("\n", s);
> +          ptf_printf(s, leaf->sub, new_prefix);
> +          free(new_prefix);
> +        }
>          break;
>        };
>
> --
> 2.5.1
>



More information about the devel mailing list