autoupdater: use safe allocation functions

Give the user a better error message when allocations fail by using our
checked allocation functions.

Signed-off-by: Tobias Schramm <tobleminer@gmail.com>
[Matthias Schiffer: squash, use safe_realloc]
This commit is contained in:
Tobias Schramm 2018-02-20 12:06:44 +01:00 committed by Matthias Schiffer
parent 3566cabef5
commit 5c6476ea58
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
3 changed files with 9 additions and 6 deletions

View File

@ -144,7 +144,8 @@ static void parse_args(int argc, char *argv[], struct settings *settings) {
if (optind < argc) { if (optind < argc) {
settings->n_mirrors = argc - optind; settings->n_mirrors = argc - optind;
settings->mirrors = malloc(settings->n_mirrors * sizeof(char *)); settings->mirrors = safe_malloc(settings->n_mirrors * sizeof(char *));
for (int i = optind; i < argc; i++) { for (int i = optind; i < argc; i++) {
settings->mirrors[i - optind] = argv[i]; settings->mirrors[i - optind] = argv[i];
} }

View File

@ -23,9 +23,9 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "hexutil.h" #include "hexutil.h"
#include "manifest.h" #include "manifest.h"
#include "util.h"
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
@ -80,14 +80,15 @@ static bool parse_rfc3339(const char *input, time_t *date) {
void parse_line(char *line, struct manifest *m, const char *branch, const char *image_name) { void parse_line(char *line, struct manifest *m, const char *branch, const char *image_name) {
if (m->sep_found) { if (m->sep_found) {
ecdsa_signature_t *sig = malloc(sizeof(ecdsa_signature_t)); ecdsa_signature_t *sig = safe_malloc(sizeof(ecdsa_signature_t));
if (!parsehex(sig, line, sizeof(*sig))) { if (!parsehex(sig, line, sizeof(*sig))) {
free(sig); free(sig);
fprintf(stderr, "autoupdater: warning: garbage in signature area: %s\n", line); fprintf(stderr, "autoupdater: warning: garbage in signature area: %s\n", line);
return; return;
} }
m->n_signatures++; m->n_signatures++;
m->signatures = realloc(m->signatures, m->n_signatures * sizeof(ecdsa_signature_t *)); m->signatures = safe_realloc(m->signatures, m->n_signatures * sizeof(ecdsa_signature_t *));
m->signatures[m->n_signatures - 1] = sig; m->signatures[m->n_signatures - 1] = sig;
} else if (strcmp(line, "---") == 0) { } else if (strcmp(line, "---") == 0) {
m->sep_found = true; m->sep_found = true;

View File

@ -27,6 +27,7 @@
#include "settings.h" #include "settings.h"
#include "hexutil.h" #include "hexutil.h"
#include "util.h"
#include <uci.h> #include <uci.h>
@ -97,7 +98,7 @@ static const char ** load_string_list(struct uci_context *ctx, struct uci_sectio
i++; i++;
*len = i; *len = i;
const char **ret = malloc(i * sizeof(char *)); const char **ret = safe_malloc(i * sizeof(char *));
i = 0; i = 0;
uci_foreach_element(&o->v.list, e) uci_foreach_element(&o->v.list, e)
@ -159,7 +160,7 @@ void load_settings(struct settings *settings) {
settings->mirrors = load_string_list(ctx, branch, "mirror", &settings->n_mirrors); settings->mirrors = load_string_list(ctx, branch, "mirror", &settings->n_mirrors);
const char **pubkeys_str = load_string_list(ctx, branch, "pubkey", &settings->n_pubkeys); const char **pubkeys_str = load_string_list(ctx, branch, "pubkey", &settings->n_pubkeys);
settings->pubkeys = malloc(settings->n_pubkeys * sizeof(ecc_25519_work_t)); settings->pubkeys = safe_malloc(settings->n_pubkeys * sizeof(ecc_25519_work_t));
size_t ignored_keys = 0; size_t ignored_keys = 0;
for (size_t i = 0; i < settings->n_pubkeys; i++) { for (size_t i = 0; i < settings->n_pubkeys; i++) {
ecc_int256_t pubkey_packed; ecc_int256_t pubkey_packed;