tree-wide: be more careful with the type of array sizes

Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.

Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.

So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.

This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:

1. strv_length()' return type becomes size_t

2. the unit file changes array size becomes size_t

3. DNS answer and query array sizes become size_t

Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
This commit is contained in:
Lennart Poettering
2018-04-27 14:29:06 +02:00
parent 545673d4b0
commit da6053d0a7
57 changed files with 288 additions and 290 deletions
+7 -7
View File
@@ -2098,7 +2098,7 @@ static int get_default(int argc, char *argv[], void *userdata) {
static int set_default(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *unit = NULL;
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
size_t n_changes = 0;
int r;
assert(argc >= 2);
@@ -6115,7 +6115,7 @@ static int enable_unit(int argc, char *argv[], void *userdata) {
_cleanup_strv_free_ char **names = NULL;
const char *verb = argv[0];
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
size_t n_changes = 0;
int carries_install_info = -1;
bool ignore_carries_install_info = arg_quiet;
int r;
@@ -6311,7 +6311,7 @@ static int enable_unit(int argc, char *argv[], void *userdata) {
if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) {
sd_bus *bus;
unsigned len, i;
size_t len, i;
r = acquire_bus(BUS_MANAGER, &bus);
if (r < 0)
@@ -6341,7 +6341,7 @@ static int add_dependency(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *target = NULL;
const char *verb = argv[0];
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
size_t n_changes = 0;
UnitDependency dep;
int r = 0;
@@ -6422,7 +6422,7 @@ finish:
static int preset_all(int argc, char *argv[], void *userdata) {
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
size_t n_changes = 0;
int r;
if (install_client_side()) {
@@ -6477,7 +6477,7 @@ finish:
static int show_installation_targets_client_side(const char *name) {
UnitFileChange *changes = NULL;
unsigned n_changes = 0, i;
size_t n_changes = 0, i;
UnitFileFlags flags;
char **p;
int r;
@@ -6809,7 +6809,7 @@ static int run_editor(char **paths) {
const char **args;
char *editor, **editor_args = NULL;
char **tmp_path, **original_path, *p;
unsigned n_editor_args = 0, i = 1;
size_t n_editor_args = 0, i = 1;
size_t argc;
argc = strv_length(paths)/2 + 1;