Project

General

Profile

Actions

Bug #4993

open

gsm_septets2octets: warning: use of NULL ‘rdata’ where non-null expected [CWE-690] [-Wanalyzer-null-argument]

Added by laforge about 3 years ago.

Status:
New
Priority:
Low
Assignee:
Category:
libosmogsm
Target version:
-
Start date:
01/29/2021
Due date:
% Done:

0%

Spec Reference:

Description

When using gcc-10 with "-fanalyzer", we get the following report:

gsm_utils.c: In function ‘gsm_septets2octets’:
gsm_utils.c:340:3: warning: use of NULL ‘rdata’ where non-null expected [CWE-690] [-Wanalyzer-null-argument]
  340 |   memcpy(data, rdata, septet_len);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ‘gsm_7bit_encode_n’: events 1-3
    |
    |  378 | int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets)
    |      |     ^~~~~~~~~~~~~~~~~
    |      |     |
    |      |     (1) entry to ‘gsm_7bit_encode_n’
    |......
    |  385 |  uint8_t *rdata = calloc(strlen(data) * 2, sizeof(uint8_t));
    |      |                          ~~~~~~~~~~~~
    |      |                          |
    |      |                          (2) allocated here
    |  386 |  y = gsm_septet_encode(rdata, data);
    |      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |      |
    |      |      (3) calling ‘gsm_septet_encode’ from ‘gsm_7bit_encode_n’
    |
    +--> ‘gsm_septet_encode’: events 4-8
           |
           |  292 | int gsm_septet_encode(uint8_t *result, const char *data)
           |      |     ^~~~~~~~~~~~~~~~~
           |      |     |
           |      |     (4) entry to ‘gsm_septet_encode’
           |......
           |  296 |  for (i = 0; i < strlen(data); i++) {
           |      |  ~~~             ~~~~~~~~~~~~
           |      |  |               |
           |      |  |               (5) following ‘false’ branch (when ‘data’ is non-NULL)...
           |      |  |               (6) ...to here
           |      |  (7) following ‘false’ branch...
           |......
           |  318 |  return y;
           |      |         ~
           |      |         |
           |      |         (8) ...to here
           |
    <------+
    |
  ‘gsm_7bit_encode_n’: events 9-10
    |
    |  386 |  y = gsm_septet_encode(rdata, data);
    |      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |      |
    |      |      (9) returning to ‘gsm_7bit_encode_n’ from ‘gsm_septet_encode’
    |......
    |  396 |  o = gsm_septets2octets(result, rdata, y, 0);
    |      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |      |
    |      |      (10) calling ‘gsm_septets2octets’ from ‘gsm_7bit_encode_n’
    |
    +--> ‘gsm_septets2octets’: events 11-19
           |
           |  327 | int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding)
           |      |     ^~~~~~~~~~~~~~~~~~
           |      |     |
           |      |     (11) entry to ‘gsm_septets2octets’
           |......
           |  334 |  if (padding) {
           |      |     ~
           |      |     |
           |      |     (12) following ‘false’ branch (when ‘padding == 0’)...
           |......
           |  340 |   memcpy(data, rdata, septet_len);
           |      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |   |
           |      |   (13) ...to here
           |      |   (14) following ‘false’ branch (when ‘data’ is non-NULL)...
           |      |   (15) ...to here
           |      |   (16) assuming ‘rdata’ is NULL
           |      |   (17) following ‘true’ branch (when ‘rdata’ is NULL)...
           |      |   (18) ...to here
           |      |   (19) argument 2 (‘rdata’) NULL where non-null expected
           |
In file included from ../../include/osmocom/core/utils.h:6,
                 from gsm_utils.c:82:
/usr/include/string.h:43:14: note: argument 2 of ‘memcpy’ must be non-null

There's also a couple of others in that file:

gsm_utils.c:340:3: warning: use of NULL ‘data’ where non-null expected [CWE-690] [-Wanalyzer-null-argument]

gsm_utils.c: In function ‘gsm_7bit_encode_n’:
gsm_utils.c:401:2: warning: double-‘free’ of ‘rdata’ [CWE-415] [-Wanalyzer-double-free]

gsm_utils.c:369:9: warning: leak of ‘rdata’ [CWE-401] [-Wanalyzer-malloc-leak]

No data to display

Actions

Also available in: Atom PDF

Add picture from clipboard (Maximum size: 48.8 MB)