bugGNU roff - Bugs: bug #62547, libdriver/input.cpp: allocation of...

 
 

bug #62547: libdriver/input.cpp: allocation of zero sized arrays, compiler warning [-Walloc-zero]

Submitter:  Bjarni Ingi Gislason <bjarniig>
Submitted:  Mon 30 May 2022 07:38:33 PM UTC
   
 
Category:  Core Severity:  2 - Minor
Item Group:  Lint Status:  Invalid
Privacy:  Public Assigned to:  gbranden
Open/Closed:  Closed Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 04 Apr 2023 12:41:12 AM UTC, comment #2: 

My gcc does not warn about this even though it does with bug #62398.

Inspecting the code, the reason is obvious.


IntArray::IntArray(const size_t n)
{
  if (n <= 0)
    fatal("number of integers to be allocated must be > 0");
  num_allocated = n;
  data = new IntArg[num_allocated];
  num_stored = 0;
}


The allocation is unreachable if `n` is zero.

Resolving as invalid.

G. Branden Robinson <gbranden>
Group administrator
Tue 31 May 2022 04:57:06 AM UTC, comment #1: 

As with bug #62398, unless this issue causes operational problems it is of no worse than "minor" severity.

G. Branden Robinson <gbranden>
Group administrator
Mon 30 May 2022 07:38:33 PM UTC, original submission:  

Subject: libdriver/input.cpp: allocation of zero sized arrays, compiler
warning [-Walloc-zero]

  Part of warnings from g++ (version (Debian 12.1.0-2) 12.1.0):

  CXX      src/libs/libdriver/input.o
In constructor 'IntArray::IntArray(size_t)',
    inlined from 'IntArray* get_D_fixed_args(size_t)' at ../src/libs/libdriver/input.cpp:667:39:
../src/libs/libdriver/input.cpp:506:34: warning: argument 1 value is zero [-Walloc-zero]
  506 |   data = new IntArg[num_allocated];
      |                                  ^
In file included from /usr/include/c++/12/bits/stl_iterator.h:82,
                 from /usr/include/c++/12/bits/stl_algobase.h:67,
                 from /usr/include/c++/12/bits/specfun.h:45,
                 from /usr/include/c++/12/cmath:1935,
                 from /usr/include/c++/12/math.h:36,
                 from ./lib/math.h:41,
                 from ../src/include/driver.h:27,
                 from ../src/libs/libdriver/input.cpp:239:
/usr/include/c++/12/new: In function 'IntArray* get_D_fixed_args(size_t)':
/usr/include/c++/12/new:128:26: note: in a call to allocation function 'void* operator new [](std::size_t)' declared here
  128 | _GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
      |                          ^~~~~~~~
In constructor 'IntArray::IntArray(size_t)',
    inlined from 'IntArray* get_D_fixed_args_odd_dummy(size_t)' at ../src/libs/libdriver/input.cpp:693:39:
../src/libs/libdriver/input.cpp:506:34: warning: argument 1 value is zero [-Walloc-zero]
  506 |   data = new IntArg[num_allocated];
      |                                  ^
/usr/include/c++/12/new: In function 'IntArray* get_D_fixed_args_odd_dummy(size_t)':
/usr/include/c++/12/new:128:26: note: in a call to allocation function 'void* operator new [](std::size_t)' declared here
  128 | _GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
      |                          ^~~~~~~~
  CXX      src/libs/libdriver/printer.o

###

  The conserning code (line 506 ...):

{
  if (n <= 0)
    fatal("number of integers to be allocated must be > 0");
  num_allocated = n;
  data = new IntArg[num_allocated];
  num_stored = 0;
}

  and (lines 667)

{
  if (number <= 0)
    fatal("requested number of arguments must be > 0");
  IntArray *args = new IntArray(number);
  for (size_t i = 0; i < number; i++)
    args->append(get_integer_arg());
  skip_line_D();
  return args;
}

  and (lines 693)

{
  if (number <= 0)
    fatal("requested number of arguments must be > 0");
  IntArray *args = new IntArray(number);
  for (size_t i = 0; i < number; i++)
    args->append(get_integer_arg());
  if (odd(number)) {
    IntArray *a = get_possibly_integer_args();
    if (a->len() > 1)
      error("too many arguments");
    delete a;
  }
  skip_line_D();
  return args;
}

###

  An if-else (selection) construct is better for the compiler.

  I have not got the warning in my branch.  I have added "#include
<config.h>" as the first line (and also in "printer.cpp") for the use with
GNULIB.

  See also bug #62398.

Bjarni Ingi Gislason <bjarniig>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by gbranden (Posted a comment)
  • -email is unavailable- added by bjarniig (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-04-04 gbranden StatusNone Invalid
        Assigned toNone gbranden
        Open/ClosedOpen Closed
    2022-05-31 gbranden Severity3 - Normal 2 - Minor

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code