Fix a possible access to uninitialized memory
[src/app-framework-main.git] / src / wgtpkg-files.c
index 8840fa9..cce6a04 100644 (file)
@@ -1,5 +1,7 @@
 /*
- Copyright 2015 IoT.bzh
+ Copyright 2015, 2016, 2017 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
-#include <syslog.h>
 #include <dirent.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <unistd.h>
+#include <limits.h>
 
-#include "wgtpkg.h"
+#include "verbose.h"
+#include "wgtpkg-workdir.h"
+#include "wgtpkg-files.h"
 
 struct fdb {
        unsigned int count;
@@ -45,7 +50,7 @@ static unsigned int what_signature(const char *name)
                return UINT_MAX;
 
        len = sizeof(distributor_file_prefix)-1;
-       if (memcmp(name, distributor_file_prefix, len))
+       if (strncmp(name, distributor_file_prefix, len))
                return 0;
        if (name[len] <= '0' || name[len] > '9')
                return 0;
@@ -53,7 +58,7 @@ static unsigned int what_signature(const char *name)
        while ('0' <= name[len] && name[len] <= '9') {
                nid = 10 * id + (unsigned int)(name[len++] - '0');
                if (nid < id || nid == UINT_MAX) {
-                       syslog(LOG_WARNING, "number too big for %s", name);
+                       WARNING("number too big for %s", name);
                        return 0;
                }
                id = nid;
@@ -94,7 +99,7 @@ static struct filedesc *get_filedesc(const char *name, int create)
        /* allocations */
        grow = realloc(allfiles.files, (allfiles.count + 1) * sizeof(struct filedesc *));
        if (grow == NULL) {
-               syslog(LOG_ERR, "realloc failed in get_filedesc");
+               ERROR("realloc failed in get_filedesc");
                return NULL;
        }
        allfiles.files = grow;
@@ -102,7 +107,7 @@ static struct filedesc *get_filedesc(const char *name, int create)
        if (sig) {
                grow = realloc(allsignatures.files, (allsignatures.count + 1) * sizeof(struct filedesc *));
                if (grow == NULL) {
-                       syslog(LOG_ERR, "second realloc failed in get_filedesc");
+                       ERROR("second realloc failed in get_filedesc");
                        return NULL;
                }
                allsignatures.files = grow;
@@ -110,7 +115,7 @@ static struct filedesc *get_filedesc(const char *name, int create)
 
        result = malloc(sizeof(struct filedesc) + strlen(name));
        if (!result) {
-               syslog(LOG_ERR, "calloc failed in get_filedesc");
+               ERROR("calloc failed in get_filedesc");
                return NULL;
        }
 
@@ -148,7 +153,7 @@ static struct filedesc *file_add(const char *name, enum entrytype type)
        else if (desc->type == type_unset)
                desc->type = type;
        else {
-               syslog(LOG_ERR, "redeclaration of %s in file_add", name);
+               ERROR("redeclaration of %s in file_add", name);
                errno = EEXIST;
                desc = NULL;
        }
@@ -227,7 +232,7 @@ struct filedesc *create_signature(unsigned int number)
                len = asprintf(&name, "%s%u%s", distributor_file_prefix, number, distributor_file_suffix);
 
        if (len < 0)
-               syslog(LOG_ERR, "asprintf failed in create_signature");
+               ERROR("asprintf failed in create_signature");
        else {
                assert(len > 0);
                result = file_of_name(name);
@@ -247,20 +252,21 @@ void file_clear_flags()
                allfiles.files[i]->flags &= flag_signature;
 }
 
-static int fill_files_rec(char name[PATH_MAX], int offset)
+static int fill_files_rec(char name[PATH_MAX], unsigned offset)
 {
-       int len, err, fd;
+       int err, fd;
+       unsigned len;
        DIR *dir;
        struct dirent *ent;
 
        fd = openat(workdirfd, offset ? name : ".", O_DIRECTORY|O_RDONLY);
        if (fd < 0) {
-               syslog(LOG_ERR, "openat %.*s failed in fill_files_rec", offset, name);
+               ERROR("openat %.*s failed in fill_files_rec", offset, name);
                return -1;
        }
        dir = fdopendir(fd);
        if (!dir) {
-               syslog(LOG_ERR, "opendir %.*s failed in fill_files_rec", offset, name);
+               ERROR("opendir %.*s failed in fill_files_rec", offset, name);
                close(fd);
                return -1;
        }
@@ -269,13 +275,13 @@ static int fill_files_rec(char name[PATH_MAX], int offset)
 
        ent = readdir(dir);
        while (ent != NULL) {
-               len = strlen(ent->d_name);
+               len = (unsigned)strlen(ent->d_name);
                if (ent->d_name[0] == '.' && (len == 1 || 
                        (ent->d_name[1] == '.' && len == 2)))
                        ;
                else if (offset + len >= PATH_MAX) {
                        closedir(dir);
-                       syslog(LOG_ERR, "name too long in fill_files_rec");
+                       ERROR("name too long in fill_files_rec");
                        errno = ENAMETOOLONG;
                        return -1;
                } else {