From 24c3550feb1c05c0b975768e0eed369f15f3b4c2 Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Tue, 23 Jun 2015 20:32:41 -0400 Subject: [PATCH] all: Ensure all malloc() failures are caught and logged. --- backend_canonselphy.c | 11 +++++++++-- backend_citizencw01.c | 10 ++++++++-- backend_common.c | 23 ++++++++++++++++++++++- backend_dnpds40.c | 8 +++++++- backend_kodak1400.c | 11 +++++++++-- backend_kodak605.c | 4 +++- backend_kodak6800.c | 15 +++++++++++++-- backend_mitsu70x.c | 4 +++- backend_mitsu9550.c | 4 +++- backend_shinkos1245.c | 8 +++++--- backend_shinkos2145.c | 17 +++++++++++++++-- backend_sonyupdr150.c | 4 +++- 12 files changed, 100 insertions(+), 19 deletions(-) diff --git a/backend_canonselphy.c b/backend_canonselphy.c index 4fc9b17..ea6225d 100644 --- a/backend_canonselphy.c +++ b/backend_canonselphy.c @@ -527,14 +527,21 @@ struct canonselphy_ctx { static void *canonselphy_init(void) { struct canonselphy_ctx *ctx = malloc(sizeof(struct canonselphy_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!\n"); return NULL; + } memset(ctx, 0, sizeof(struct canonselphy_ctx)); /* Static initialization */ setup_paper_codes(); ctx->buffer = malloc(MAX_HEADER); + if (!ctx->buffer) { + ERROR("Memory Allocation Failure!\n"); + free(ctx); + ctx = NULL; + } return ctx; } @@ -944,7 +951,7 @@ top: struct dyesub_backend canonselphy_backend = { .name = "Canon SELPHY CP/ES", - .version = "0.86", + .version = "0.87", .uri_prefix = "canonselphy", .init = canonselphy_init, .attach = canonselphy_attach, diff --git a/backend_citizencw01.c b/backend_citizencw01.c index 07df617..e40d189 100644 --- a/backend_citizencw01.c +++ b/backend_citizencw01.c @@ -228,6 +228,10 @@ static uint8_t *cw01_resp_cmd(struct cw01_ctx *ctx, i = atoi(tmp); /* Length of payload in bytes, possibly padded */ respbuf = malloc(i); + if (!respbuf) { + ERROR("Memory Allocation Failure!\n"); + return NULL; + } /* Read in the actual response */ ret = read_data(ctx->dev, ctx->endp_up, @@ -279,8 +283,10 @@ static int cw01_query_serno(struct libusb_device_handle *dev, uint8_t endp_up, u static void *cw01_init(void) { struct cw01_ctx *ctx = malloc(sizeof(struct cw01_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!\n"); return NULL; + } memset(ctx, 0, sizeof(struct cw01_ctx)); return ctx; @@ -861,7 +867,7 @@ static int cw01_cmdline_arg(void *vctx, int argc, char **argv) /* Exported */ struct dyesub_backend cw01_backend = { .name = "Citizen CW-01", - .version = "0.09", + .version = "0.10", .uri_prefix = "citizencw01", .cmdline_usage = cw01_cmdline, .cmdline_arg = cw01_cmdline_arg, diff --git a/backend_common.c b/backend_common.c index fc101ad..ec2c857 100644 --- a/backend_common.c +++ b/backend_common.c @@ -27,7 +27,7 @@ #include "backend_common.h" -#define BACKEND_VERSION "0.53" +#define BACKEND_VERSION "0.54" #ifndef URI_PREFIX #error "Must Define URI_PREFIX" #endif @@ -48,6 +48,11 @@ static char *get_device_id(struct libusb_device_handle *dev) int iface = 0; char *buf = malloc(ID_BUF_SIZE + 1); + if (!buf) { + ERROR("Memory allocation failure (%d bytes)\n", ID_BUF_SIZE+1); + return NULL; + } + if (libusb_kernel_driver_active(dev, iface)) libusb_detach_kernel_driver(dev, iface); @@ -287,6 +292,11 @@ static char from_hex(char ch) { static char *url_encode(char *str) { char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf; + if (!buf) { + ERROR("Memory allocation failure (%d bytes)\n", (int) strlen(str)*3 + 1); + return NULL; + } + while (*pstr) { if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') *pbuf++ = *pstr; @@ -301,6 +311,12 @@ static char *url_encode(char *str) { } static char *url_decode(char *str) { char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf; + + if (!buf) { + ERROR("Memory allocation failure (%d bytes)\n", (int) strlen(str) + 1); + return NULL; + } + while (*pstr) { if (*pstr == '%') { if (pstr[1] && pstr[2]) { @@ -393,6 +409,11 @@ static int print_scan_output(struct libusb_device *device, char *product2 = url_decode(product); char *manuf3 = url_decode(manuf); descr = malloc(256); + if (!descr) { + ERROR("Memory allocation failure (%d bytes)\n", 256); + return found; + } + sprintf(descr, "%s %s", manuf3, product2); free(product2); free(manuf3); diff --git a/backend_dnpds40.c b/backend_dnpds40.c index 90e642f..ecd2904 100644 --- a/backend_dnpds40.c +++ b/backend_dnpds40.c @@ -241,6 +241,10 @@ static uint8_t * dnpds40_resp_cmd(struct dnpds40_ctx *ctx, i = atoi(tmp); /* Length of payload in bytes, possibly padded */ respbuf = malloc(i); + if (!respbuf) { + ERROR("Memory allocation failure (%d bytes)!\n", i); + return NULL; + } /* Read in the actual response */ ret = read_data(ctx->dev, ctx->endp_up, @@ -292,8 +296,10 @@ static int dnpds40_query_serno(struct libusb_device_handle *dev, uint8_t endp_up static void *dnpds40_init(void) { struct dnpds40_ctx *ctx = malloc(sizeof(struct dnpds40_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory allocation failure (%d bytes)!\n", (int)sizeof(struct dnpds40_ctx)); return NULL; + } memset(ctx, 0, sizeof(struct dnpds40_ctx)); ctx->type = P_ANY; diff --git a/backend_kodak1400.c b/backend_kodak1400.c index b865473..6c187dc 100644 --- a/backend_kodak1400.c +++ b/backend_kodak1400.c @@ -158,6 +158,11 @@ static int kodak1400_set_tonecurve(struct kodak1400_ctx *ctx, char *fname) uint16_t *data = malloc(UPDATE_SIZE); + if (!data) { + ERROR("Memory Allocation Failure!\n"); + return -1; + } + /* Read in file */ int tc_fd = open(fname, O_RDONLY); if (tc_fd < 0) { @@ -277,8 +282,10 @@ int kodak1400_cmdline_arg(void *vctx, int argc, char **argv) static void *kodak1400_init(void) { struct kodak1400_ctx *ctx = malloc(sizeof(struct kodak1400_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!\n"); return NULL; + } memset(ctx, 0, sizeof(struct kodak1400_ctx)); return ctx; @@ -596,7 +603,7 @@ top: struct dyesub_backend kodak1400_backend = { .name = "Kodak 1400/805", - .version = "0.32", + .version = "0.33", .uri_prefix = "kodak1400", .cmdline_usage = kodak1400_cmdline, .cmdline_arg = kodak1400_cmdline_arg, diff --git a/backend_kodak605.c b/backend_kodak605.c index 30b150d..78c5150 100644 --- a/backend_kodak605.c +++ b/backend_kodak605.c @@ -95,8 +95,10 @@ enum { static void *kodak605_init(void) { struct kodak605_ctx *ctx = malloc(sizeof(struct kodak605_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!\n"); return NULL; + } memset(ctx, 0, sizeof(struct kodak605_ctx)); return ctx; diff --git a/backend_kodak6800.c b/backend_kodak6800.c index 9ff7516..f1e27d7 100644 --- a/backend_kodak6800.c +++ b/backend_kodak6800.c @@ -279,6 +279,10 @@ static int kodak6800_get_tonecurve(struct kodak6800_ctx *ctx, char *fname) int i; uint16_t *data = malloc(UPDATE_SIZE); + if (!data) { + ERROR("Memory Allocation Failure\n"); + return -1; + } INFO("Dump Tone Curve to '%s'\n", fname); @@ -384,6 +388,11 @@ static int kodak6800_set_tonecurve(struct kodak6800_ctx *ctx, char *fname) uint16_t *data = malloc(UPDATE_SIZE); uint8_t *ptr; + if (!data) { + ERROR("Memory Allocation Failure\n"); + return -1; + } + INFO("Set Tone Curve from '%s'\n", fname); /* Read in file */ @@ -630,8 +639,10 @@ static int kodak6800_cmdline_arg(void *vctx, int argc, char **argv) static void *kodak6800_init(void) { struct kodak6800_ctx *ctx = malloc(sizeof(struct kodak6800_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure\n"); return NULL; + } memset(ctx, 0, sizeof(struct kodak6800_ctx)); ctx->type = P_ANY; @@ -880,7 +891,7 @@ top: /* Exported */ struct dyesub_backend kodak6800_backend = { .name = "Kodak 6800/6850", - .version = "0.42", + .version = "0.43", .uri_prefix = "kodak6800", .cmdline_usage = kodak6800_cmdline, .cmdline_arg = kodak6800_cmdline_arg, diff --git a/backend_mitsu70x.c b/backend_mitsu70x.c index b58d780..9a91cc6 100644 --- a/backend_mitsu70x.c +++ b/backend_mitsu70x.c @@ -130,8 +130,10 @@ struct mitsu70x_hdr { static void *mitsu70x_init(void) { struct mitsu70x_ctx *ctx = malloc(sizeof(struct mitsu70x_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!\n"); return NULL; + } memset(ctx, 0, sizeof(struct mitsu70x_ctx)); return ctx; diff --git a/backend_mitsu9550.c b/backend_mitsu9550.c index 86037d5..fb79858 100644 --- a/backend_mitsu9550.c +++ b/backend_mitsu9550.c @@ -143,8 +143,10 @@ struct mitsu9550_status2 { static void *mitsu9550_init(void) { struct mitsu9550_ctx *ctx = malloc(sizeof(struct mitsu9550_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!\n"); return NULL; + } memset(ctx, 0, sizeof(struct mitsu9550_ctx)); /* Use Fast return by default in CUPS mode */ diff --git a/backend_shinkos1245.c b/backend_shinkos1245.c index 9380891..cf62859 100644 --- a/backend_shinkos1245.c +++ b/backend_shinkos1245.c @@ -955,7 +955,7 @@ static int get_tonecurve(struct shinkos1245_ctx *ctx, int type, int table, char remaining = TONE_CURVE_SIZE; data = malloc(remaining); if (!data) { - ERROR("Out of memory!\n"); + ERROR("Memory Allocation Failure!\n"); return -11; } ptr = data; @@ -1033,7 +1033,7 @@ static int set_tonecurve(struct shinkos1245_ctx *ctx, int type, int table, char remaining = TONE_CURVE_SIZE; data = malloc(remaining); if (!data) { - ERROR("Out of memory!\n"); + ERROR("Memory Allocation Failure!\n"); return -11; } ptr = data; @@ -1230,8 +1230,10 @@ int shinkos1245_cmdline_arg(void *vctx, int argc, char **argv) static void *shinkos1245_init(void) { struct shinkos1245_ctx *ctx = malloc(sizeof(struct shinkos1245_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!\n"); return NULL; + } memset(ctx, 0, sizeof(struct shinkos1245_ctx)); /* Use Fast return by default in CUPS mode */ diff --git a/backend_shinkos2145.c b/backend_shinkos2145.c index 7c73014..338a77b 100644 --- a/backend_shinkos2145.c +++ b/backend_shinkos2145.c @@ -1158,6 +1158,10 @@ static int get_tonecurve(struct shinkos2145_ctx *ctx, int type, char *fname) resp->total_size = le16_to_cpu(resp->total_size); data = malloc(resp->total_size * 2); + if (!data) { + ERROR("Memory allocation failure! (%d bytes)\n", + resp->total_size * 2); + } i = 0; while (i < resp->total_size) { @@ -1208,6 +1212,11 @@ static int set_tonecurve(struct shinkos2145_ctx *ctx, int target, char *fname) uint16_t *data = malloc(UPDATE_SIZE); + if (!data) { + ERROR("Memory allocation failure! (%d bytes)\n", + UPDATE_SIZE); + } + /* Read in file */ int tc_fd = open(fname, O_RDONLY); if (tc_fd < 0) { @@ -1401,8 +1410,12 @@ int shinkos2145_cmdline_arg(void *vctx, int argc, char **argv) static void *shinkos2145_init(void) { struct shinkos2145_ctx *ctx = malloc(sizeof(struct shinkos2145_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory allocation failure! (%d bytes)\n", + (int)sizeof(struct shinkos2145_ctx)); + return NULL; + } memset(ctx, 0, sizeof(struct shinkos2145_ctx)); /* Use Fast return by default in CUPS mode */ @@ -1758,7 +1771,7 @@ static int shinkos2145_query_serno(struct libusb_device_handle *dev, uint8_t end struct dyesub_backend shinkos2145_backend = { .name = "Shinko/Sinfonia CHC-S2145", - .version = "0.39", + .version = "0.40", .uri_prefix = "shinkos2145", .cmdline_usage = shinkos2145_cmdline, .cmdline_arg = shinkos2145_cmdline_arg, diff --git a/backend_sonyupdr150.c b/backend_sonyupdr150.c index 37783e5..f863090 100644 --- a/backend_sonyupdr150.c +++ b/backend_sonyupdr150.c @@ -59,8 +59,10 @@ struct updr150_ctx { static void* updr150_init(void) { struct updr150_ctx *ctx = malloc(sizeof(struct updr150_ctx)); - if (!ctx) + if (!ctx) { + ERROR("Memory Allocation Failure!"); return NULL; + } memset(ctx, 0, sizeof(struct updr150_ctx)); return ctx; }