[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug #62547] libdriver/input.cpp: allocation of zero sized arrays, compi
From: |
Bjarni Ingi Gislason |
Subject: |
[bug #62547] libdriver/input.cpp: allocation of zero sized arrays, compiler warning [-Walloc-zero] |
Date: |
Mon, 30 May 2022 15:38:35 -0400 (EDT) |
URL:
<https://savannah.gnu.org/bugs/?62547>
Summary: libdriver/input.cpp: allocation of zero sized
arrays, compiler warning [-Walloc-zero]
Project: GNU troff
Submitted by: bjarniig
Submitted on: Mon 30 May 2022 07:38:33 PM UTC
Category: Core
Severity: 3 - Normal
Item Group: Lint
Status: None
Privacy: Public
Assigned to: None
Open/Closed: Open
Discussion Lock: Any
Planned Release: None
_______________________________________________________
Follow-up Comments:
-------------------------------------------------------
Date: Mon 30 May 2022 07:38:33 PM UTC By: Bjarni Ingi Gislason <bjarniig>
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.
_______________________________________________________
Reply to this item at:
<https://savannah.gnu.org/bugs/?62547>
_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
- [bug #62547] libdriver/input.cpp: allocation of zero sized arrays, compiler warning [-Walloc-zero],
Bjarni Ingi Gislason <=