all: Ensure all malloc() failures are caught and logged.

This commit is contained in:
Solomon Peachy 2015-06-23 20:32:41 -04:00
parent 980fada366
commit 24c3550feb
12 changed files with 100 additions and 19 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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);

View File

@ -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;

View File

@ -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,

View File

@ -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;

View File

@ -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,

View File

@ -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;

View File

@ -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 */

View File

@ -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 */

View File

@ -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,

View File

@ -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;
}