From 2f18522602ebd32009174aa447a8394900f9f565 Mon Sep 17 00:00:00 2001 From: Dominic Radermacher Date: Mon, 19 May 2025 13:33:55 +0200 Subject: [PATCH] fix segfault for last commit --- src/libptouch.c | 40 +++++++++++++++++++++++++++++++++++++++- src/ptouch-print.c | 39 +++++++++++++++++++-------------------- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/libptouch.c b/src/libptouch.c index 9876f3a..eed8e97 100644 --- a/src/libptouch.c +++ b/src/libptouch.c @@ -163,6 +163,9 @@ int ptouch_open(ptouch_dev *ptdev) int ptouch_close(ptouch_dev ptdev) { + if (!ptdev) { + return -1; + } libusb_release_interface(ptdev->h, 0); libusb_close(ptdev->h); return 0; @@ -172,7 +175,11 @@ int ptouch_send(ptouch_dev ptdev, uint8_t *data, size_t len) { int r, tx; - if ((ptdev == NULL) || (len > 128)) { + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_send() with NULL ptdev\n")); + return -1; + } + if (len > 128) { return -1; } if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, (int)len, &tx, 0)) != 0) { @@ -228,6 +235,11 @@ int ptouch_enable_packbits(ptouch_dev ptdev) /* print information command */ int ptouch_info_cmd(ptouch_dev ptdev, int size_x) { + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_info_cmd() with NULL ptdev\n")); + return -1; + } + /* 1B 69 7A {n1} {n2} {n3} {n4} {n5} {n6} {n7} {n8} {n9} {n10} */ uint8_t cmd[] = "\x1b\x69\x7a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; @@ -265,6 +277,10 @@ int ptouch_send_precut_cmd(ptouch_dev ptdev, int precut) int ptouch_rasterstart(ptouch_dev ptdev) { + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_rasterstart() with NULL ptdev\n")); + return -1; + } /* 1B 69 52 01 = Select graphics transfer mode = Raster */ char cmd[] = "\x1b\x69\x52\x01"; /* 1B 69 61 01 = switch mode (0=esc/p, 1=raster mode) */ @@ -292,6 +308,11 @@ int ptouch_ff(ptouch_dev ptdev) /* finish print and either cut or leave tape in machine */ int ptouch_finalize(ptouch_dev ptdev, int chain) { + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_finalize() with NULL ptdev\n")); + return -1; + } + char cmd_eject[]="\x1a"; /* Print command with feeding */ char cmd_chain[]="\x0c"; /* Print command (no cut) */ @@ -321,6 +342,11 @@ int ptouch_getstatus(ptouch_dev ptdev) int i, r, tx=0, tries=0; struct timespec w; + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_getstatus() with NULL ptdev\n")); + return -1; + } + ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd)); while (tx == 0) { w.tv_sec=0; @@ -372,11 +398,19 @@ int ptouch_getstatus(ptouch_dev ptdev) size_t ptouch_get_tape_width(ptouch_dev ptdev) { + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_get_tape_width() with NULL ptdev\n")); + return 0; + } return ptdev->tape_width_px; } size_t ptouch_get_max_width(ptouch_dev ptdev) { + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_get_max_width() with NULL ptdev\n")); + return 0; + } return ptdev->devinfo->max_px; } @@ -385,6 +419,10 @@ int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, size_t len) uint8_t buf[64]; int rc; + if (!ptdev) { + fprintf(stderr, _("debug: called ptouch_sendraster() with NULL ptdev\n")); + return -1; + } if (len > (size_t)(ptdev->devinfo->max_px / 8)) { return -1; } diff --git a/src/ptouch-print.c b/src/ptouch-print.c index 7ee1bfd..2a40e94 100644 --- a/src/ptouch-print.c +++ b/src/ptouch-print.c @@ -58,7 +58,7 @@ int verbose = 0; int fontsize = 0; bool debug = false; bool chain = false; -int forced_print_width = 0; +int forced_tape_width = 0; /* -------------------------------------------------------------------- -------------------------------------------------------------------- */ @@ -473,7 +473,7 @@ int parse_args(int argc, char **argv) } } else if (strcmp(&argv[i][1], "-force-tape-width") == 0) { if (i+1 < argc) { - forced_print_width = strtol(argv[++i], NULL, 10); + forced_tape_width = strtol(argv[++i], NULL, 10); } else { usage(argv[0]); } @@ -520,15 +520,15 @@ int parse_args(int argc, char **argv) usage(argv[0]); } } - if (forced_print_width && !save_png) { - forced_print_width = 0; + if (forced_tape_width && !save_png) { + forced_tape_width = 0; } return i; } int main(int argc, char *argv[]) { - int i, lines = 0, copies = 1, print_width = 0; + int lines = 0, copies = 1, print_width = 0; char *line[MAX_LINES]; gdImage *im = NULL; gdImage *out = NULL; @@ -537,13 +537,11 @@ int main(int argc, char *argv[]) setlocale(LC_ALL, ""); bindtextdomain("ptouch-print", "/usr/share/locale/"); textdomain("ptouch-print"); - i = parse_args(argc, argv); + int i = parse_args(argc, argv); if (i != argc) { usage(argv[0]); } - int tape_width = ptouch_get_tape_width(ptdev); - int max_print_width = ptouch_get_max_width(ptdev); - if (!forced_print_width) { + if (!forced_tape_width) { if ((ptouch_open(&ptdev)) < 0) { return 5; } @@ -554,13 +552,14 @@ int main(int argc, char *argv[]) printf(_("ptouch_getstatus() failed\n")); return 1; } - print_width = tape_width; - } else { - print_width = forced_print_width; - } - // do not try to print more pixels than printhead has - if (print_width > max_print_width) { - print_width = max_print_width; + print_width = ptouch_get_tape_width(ptdev); + int max_print_width = ptouch_get_max_width(ptdev); + // do not try to print more pixels than printhead has + if (print_width > max_print_width) { + print_width = max_print_width; + } + } else { // --forced_tape_width together with --writepng + print_width = forced_tape_width; } for (i = 1; i < argc; ++i) { if (*argv[i] != '-') { @@ -579,7 +578,7 @@ int main(int argc, char *argv[]) usage(argv[0]); } } else if (strcmp(&argv[i][1], "-force-tape-width") == 0) { - if (forced_print_width && save_png) { + if (forced_tape_width && save_png) { ++i; continue; } else { @@ -589,8 +588,8 @@ int main(int argc, char *argv[]) i++; continue; } else if (strcmp(&argv[i][1], "-info") == 0) { - printf(_("maximum printing width for this printer is %ipx\n"), max_print_width); - printf(_("maximum printing width for this tape is %ipx\n"), tape_width); + printf(_("maximum printing width for this printer is %ldpx\n"), ptouch_get_max_width(ptdev)); + printf(_("maximum printing width for this tape is %ldpx\n"), ptouch_get_tape_width(ptdev)); printf("media type = %02x (%s)\n", ptdev->status->media_type, pt_mediatype(ptdev->status->media_type)); printf("media width = %d mm\n", ptdev->status->media_width); printf("tape color = %02x (%s)\n", ptdev->status->tape_color, pt_tapecolor(ptdev->status->tape_color)); @@ -663,7 +662,7 @@ int main(int argc, char *argv[]) if (im != NULL) { gdImageDestroy(im); } - if (!forced_print_width) { + if (!forced_tape_width) { ptouch_close(ptdev); } libusb_exit(NULL);