More cppcheck-driven fixes. Mainly a bunch of memory leaks.

This commit is contained in:
Solomon Peachy 2014-02-11 20:11:33 -05:00
parent 5a4ff83750
commit 5f8780cc3c
8 changed files with 111 additions and 76 deletions

View File

@ -22,7 +22,7 @@ $(BACKENDS): $(EXEC_NAME)
ln -sf $(EXEC_NAME) $@
cppcheck:
cppcheck -q -v --std=c99 --enable=all -DURI_PREFIX=\"$(BACKEND_NAME)\" $(SOURCES)
cppcheck -q -v --std=c99 --enable=all -I/usr/include -DURI_PREFIX=\"$(BACKEND_NAME)\" $(SOURCES)
install:
install -o root -m 700 $(EXEC_NAME) $(CUPS_BACKEND_DIR)/$(BACKEND_NAME)

View File

@ -266,7 +266,6 @@ static int print_scan_output(struct libusb_device *device,
} else if (backend->query_serno) {
/* XXX this is ... a cut-n-paste hack */
uint8_t endp_up, endp_down;
int iface = 0;
struct libusb_config_descriptor *config;
@ -277,6 +276,7 @@ static int print_scan_output(struct libusb_device *device,
so we should just skip over it... */
if (!libusb_claim_interface(dev, iface)) {
int i;
uint8_t endp_up, endp_down;
libusb_get_active_config_descriptor(device, &config);
for (i = 0 ; i < config->interface[0].altsetting[0].bNumEndpoints ; i++) {
if ((config->interface[0].altsetting[0].endpoint[i].bmAttributes & 3) == LIBUSB_TRANSFER_TYPE_BULK) {

View File

@ -93,7 +93,7 @@ static void dnpds40_build_cmd(struct dnpds40_cmd *cmd, char *arg1, char *arg2, u
memcpy(cmd->arg2, arg2, min(strlen(arg2), sizeof(cmd->arg2)));
if (arg3_len) {
char buf[9];
snprintf(buf, sizeof(buf), "%08d", arg3_len);
snprintf(buf, sizeof(buf), "%08u", arg3_len);
memcpy(cmd->arg3, buf, 8);
}
@ -443,7 +443,7 @@ static int dnpds40_read_parse(void *vctx, int data_fd) {
if (matte != ctx->last_matte)
ctx->buf_needed = 2;
DEBUG("dpi %d matte %d(%d) mcut %d bufs %d\n",
DEBUG("dpi %u matte %u(%u) mcut %u bufs %d\n",
dpi, matte, ctx->last_matte, multicut, ctx->buf_needed);
/* Track if our last print was matte */

View File

@ -88,7 +88,6 @@ static int send_plane(struct kodak1400_ctx *ctx,
uint8_t planeno, uint8_t *planedata,
uint8_t *cmdbuf)
{
int i;
uint16_t temp16;
int ret;
@ -122,6 +121,7 @@ static int send_plane(struct kodak1400_ctx *ctx,
return ret;
if (planedata) {
int i;
for (i = 0 ; i < ctx->hdr.rows ; i++) {
if ((ret = send_data(ctx->dev, ctx->endp_down,
planedata + i * ctx->hdr.columns,
@ -152,7 +152,7 @@ static int kodak1400_set_tonecurve(struct kodak1400_ctx *ctx, char *fname)
uint8_t cmdbuf[8];
uint8_t respbuf[64];
int ret, num = 0;
int ret = 0, num = 0;
INFO("Set Tone Curve from '%s'\n", fname);
@ -160,10 +160,14 @@ static int kodak1400_set_tonecurve(struct kodak1400_ctx *ctx, char *fname)
/* Read in file */
int tc_fd = open(fname, O_RDONLY);
if (tc_fd < 0)
return -1;
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE)
return -2;
if (tc_fd < 0) {
ret = -1;
goto done;
}
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) {
ret = -2;
goto done;
}
close(tc_fd);
/* Byteswap data to printer's format */
@ -171,28 +175,32 @@ static int kodak1400_set_tonecurve(struct kodak1400_ctx *ctx, char *fname)
data[ret] = cpu_to_le16(be16_to_cpu(data[ret]));
}
/* Null-terminate */
memset(((uint8_t*)data)+UPDATE_SIZE-16, 0x0, 16);
memset(data + (UPDATE_SIZE-16)/2, 0, 16);
/* Clear tables */
memset(cmdbuf, 0, sizeof(cmdbuf));
cmdbuf[0] = 0x1b;
cmdbuf[1] = 0xa2;
if ((ret = send_data(dev, endp_down,
cmdbuf, 2)))
return -1;
cmdbuf, 2))) {
ret = -3;
goto done;
}
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
return ret;
goto done;
if (num != 8) {
ERROR("Short Read! (%d/%d)\n", num, 8);
return ret;
ret = -4;
goto done;
}
if (respbuf[1] != 0x01) {
ERROR("Received unexpected response\n");
return ret;
ret = -5;
goto done;
}
/* Set up the update command */
@ -205,33 +213,34 @@ static int kodak1400_set_tonecurve(struct kodak1400_ctx *ctx, char *fname)
cmdbuf[5] = 0x10; /* 06 10 == UPDATE_SIZE */
if ((ret = send_data(dev, endp_down,
cmdbuf, 6)))
return -1;
goto done;
/* Send the payload over */
if ((ret = send_data(dev, endp_down,
(uint8_t *) data, UPDATE_SIZE))) {
return ret;
}
(uint8_t *) data, UPDATE_SIZE)))
goto done;
/* get the response */
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
return ret;
goto done;
if (num != 8) {
ERROR("Short Read! (%d/%d)\n", num, 8);
return ret;
ret = -6;
goto done;
}
if (respbuf[1] != 0x00) {
ERROR("Received unexpected response!\n");
return ret;
ret = -7;
goto done;
}
done:
free(data);
return 0;
return ret;
}
static void kodak1400_cmdline(void)
@ -351,9 +360,9 @@ static int kodak1400_read_parse(void *vctx, int data_fd) {
}
for (i = 0 ; i < ctx->hdr.rows ; i++) {
int j;
int remain;
uint8_t *ptr;
for (j = 0 ; j < 3 ; j++) {
int remain;
if (j == 0)
ptr = ctx->plane_r + i * ctx->hdr.columns;
else if (j == 1)

View File

@ -415,10 +415,14 @@ static int kodak605_set_tonecurve(struct kodak605_ctx *ctx, char *fname)
/* Read in file */
int tc_fd = open(fname, O_RDONLY);
if (tc_fd < 0)
return -1;
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE)
return -2;
if (tc_fd < 0) {
ret = -1;
goto done;
}
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) {
ret = 4;
goto done;
}
close(tc_fd);
/* Byteswap data to printer's format */
@ -444,23 +448,25 @@ static int kodak605_set_tonecurve(struct kodak605_ctx *ctx, char *fname)
if ((ret = send_data(dev, endp_down,
cmdbuf, 14)))
return -1;
goto done;
/* Get response back */
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
return ret;
goto done;
if (num != 10) {
ERROR("Short Read! (%d/%d)\n", num, 10);
return 4;
ret = 4;
goto done;
}
/* Send the data over! */
ret = send_data(dev, endp_up,
(uint8_t*)data, sizeof(data));
done:
/* We're done */
free(data);
return ret;

View File

@ -116,16 +116,17 @@ static int kodak6800_get_tonecurve(struct kodak6800_ctx *ctx, char *fname)
if ((ret = send_data(dev, endp_down,
cmdbuf, 16)))
return ret;
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
return ret;
goto done;
if (num != 51) {
ERROR("Short read! (%d/%d)\n", num, 51);
return 4;
ret = 4;
goto done;
}
/* Then we can poll the data */
@ -143,16 +144,17 @@ static int kodak6800_get_tonecurve(struct kodak6800_ctx *ctx, char *fname)
for (i = 0 ; i < 24 ; i++) {
if ((ret = send_data(dev, endp_down,
cmdbuf, 11)))
return -1;
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
return ret;
goto done;
if (num != 64) {
ERROR("Short read! (%d/%d)\n", num, 51);
return 4;
ret = 4;
goto done;
}
/* Copy into buffer */
@ -162,8 +164,10 @@ static int kodak6800_get_tonecurve(struct kodak6800_ctx *ctx, char *fname)
/* Open file and write it out */
{
int tc_fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
if (tc_fd < 0)
return -1;
if (tc_fd < 0) {
ret = 4;
goto done;
}
for (i = 0 ; i < 768; i++) {
/* Byteswap appropriately */
@ -173,7 +177,7 @@ static int kodak6800_get_tonecurve(struct kodak6800_ctx *ctx, char *fname)
close(tc_fd);
}
done:
/* We're done */
free(data);
@ -198,10 +202,14 @@ static int kodak6800_set_tonecurve(struct kodak6800_ctx *ctx, char *fname)
/* Read in file */
int tc_fd = open(fname, O_RDONLY);
if (tc_fd < 0)
return -1;
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE)
return -2;
if (tc_fd < 0) {
ret = -1;
goto done;
}
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) {
ret = -2;
goto done;
}
close(tc_fd);
/* Byteswap data to printer's format */
@ -229,16 +237,17 @@ static int kodak6800_set_tonecurve(struct kodak6800_ctx *ctx, char *fname)
if ((ret = send_data(dev, endp_down,
cmdbuf, 16)))
return -1;
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
return ret;
goto done;
if (num != 51) {
ERROR("Short read! (%d/%d)\n", num, 51);
return 4;
ret = 4;
goto done;
}
ptr = (uint8_t*) data;
@ -255,23 +264,25 @@ static int kodak6800_set_tonecurve(struct kodak6800_ctx *ctx, char *fname)
/* Send next block over */
if ((ret = send_data(dev, endp_down,
cmdbuf, count+1)))
return -1;
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
return ret;
goto done;
if (num != 51) {
ERROR("Short read! (%d/%d)\n", num, 51);
return 4;
ret = 4;
goto done;
}
};
done:
/* We're done */
free(data);
return 0;
return ret;
}
static void kodak6800_cmdline(void)

View File

@ -899,13 +899,13 @@ static int get_status(struct shinkos2145_ctx *ctx)
return 0;
INFO(" Print Counts:\n");
INFO("\tSince Paper Changed:\t%08d\n", le32_to_cpu(resp->count_paper));
INFO("\tLifetime:\t\t%08d\n", le32_to_cpu(resp->count_lifetime));
INFO("\tMaintainence:\t\t%08d\n", le32_to_cpu(resp->count_maint));
INFO("\tPrint Head:\t\t%08d\n", le32_to_cpu(resp->count_head));
INFO(" Cutter Actuations:\t%08d\n", le32_to_cpu(resp->count_cutter));
INFO(" Ribbon Remaining:\t%08d\n", le32_to_cpu(resp->count_ribbon_left));
INFO("Bank 1: 0x%02x (%s) Job %03d @ %03d/%03d (%03d remaining)\n",
INFO("\tSince Paper Changed:\t%08u\n", le32_to_cpu(resp->count_paper));
INFO("\tLifetime:\t\t%08u\n", le32_to_cpu(resp->count_lifetime));
INFO("\tMaintainence:\t\t%08u\n", le32_to_cpu(resp->count_maint));
INFO("\tPrint Head:\t\t%08u\n", le32_to_cpu(resp->count_head));
INFO(" Cutter Actuations:\t%08u\n", le32_to_cpu(resp->count_cutter));
INFO(" Ribbon Remaining:\t%08u\n", le32_to_cpu(resp->count_ribbon_left));
INFO("Bank 1: 0x%02x (%s) Job %03u @ %03u/%03u (%03u remaining)\n",
resp->bank1_status, bank_statuses(resp->bank1_status),
resp->bank1_printid,
le16_to_cpu(resp->bank1_finished),
@ -928,7 +928,7 @@ static int get_fwinfo(struct shinkos2145_ctx *ctx)
{
struct s2145_fwinfo_cmd cmd;
struct s2145_fwinfo_resp *resp = (struct s2145_fwinfo_resp *)rdbuf;
int ret, num = 0;
int num = 0;
int i;
cmd.hdr.cmd = cpu_to_le16(S2145_CMD_FWINFO);
@ -937,6 +937,7 @@ static int get_fwinfo(struct shinkos2145_ctx *ctx)
INFO("FW Information:\n");
for (i = FWINFO_TARGET_MAIN_BOOT ; i <= FWINFO_TARGET_TABLES ; i++) {
int ret;
cmd.target = i;
if ((ret = s2145_do_cmd(ctx,
@ -986,7 +987,7 @@ static int get_errorlog(struct shinkos2145_ctx *ctx)
INFO("Stored Error Events: %d entries:\n", resp->count);
for (i = 0 ; i < resp->count ; i++) {
INFO(" %02d: @ %08d prints : 0x%02x/0x%02x (%s)\n", i,
INFO(" %02d: @ %08u prints : 0x%02x/0x%02x (%s)\n", i,
le32_to_cpu(resp->items[i].print_counter),
resp->items[i].major, resp->items[i].minor,
error_codes(resp->items[i].major, resp->items[i].minor));
@ -1209,7 +1210,7 @@ static int get_tonecurve(struct shinkos2145_ctx *ctx, int type, char *fname)
resp->total_size * 2 - i,
&num);
if (ret < 0)
return ret;
goto done;
i += num;
}
@ -1223,8 +1224,10 @@ static int get_tonecurve(struct shinkos2145_ctx *ctx, int type, char *fname)
/* Open file and write it out */
{
int tc_fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
if (tc_fd < 0)
return -1;
if (tc_fd < 0) {
ret = -1;
goto done;
}
for (i = 0 ; i < 768; i++) {
/* Byteswap appropriately */
@ -1234,8 +1237,9 @@ static int get_tonecurve(struct shinkos2145_ctx *ctx, int type, char *fname)
close(tc_fd);
}
done:
free(data);
return 0;
return ret;
}
static int set_tonecurve(struct shinkos2145_ctx *ctx, int target, char *fname)
@ -1250,10 +1254,14 @@ static int set_tonecurve(struct shinkos2145_ctx *ctx, int target, char *fname)
/* Read in file */
int tc_fd = open(fname, O_RDONLY);
if (tc_fd < 0)
return -1;
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE)
return -2;
if (tc_fd < 0) {
ret = -1;
goto done;
}
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) {
ret = -2;
goto done;
}
close(tc_fd);
/* Byteswap data to local CPU.. */
for (ret = 0; ret < UPDATE_SIZE ; ret+=2) {
@ -1278,18 +1286,19 @@ static int set_tonecurve(struct shinkos2145_ctx *ctx, int target, char *fname)
sizeof(*resp),
&num)) < 0) {
ERROR("Failed to execute %s command\n", cmd_names(cmd.hdr.cmd));
return ret;
goto done;
}
/* Sent transfer */
if ((ret = send_data(ctx->dev, ctx->endp_down,
(uint8_t *) data, UPDATE_SIZE))) {
return ret;
goto done;
}
done:
free(data);
return 0;
return ret;
}
static void shinkos2145_cmdline(void)
@ -1513,7 +1522,7 @@ static int shinkos2145_read_parse(void *vctx, int data_fd) {
/* Make sure footer is sane too */
ret = read(data_fd, tmpbuf, 4);
if (ret < 0 || ret != 4) {
if (ret != 4) {
ERROR("Read failed (%d/%d/%d)\n",
ret, 4, 4);
perror("ERROR: Read failed");

View File

@ -188,7 +188,7 @@ top:
i += sizeof(uint32_t);
if (dyesub_debug)
DEBUG("Sending %d bytes to printer @ %d\n", len, i);
DEBUG("Sending %u bytes to printer @ %i\n", len, i);
if ((ret = send_data(ctx->dev, ctx->endp_down,
ctx->databuf + i, len)))
return ret;