From 538b8f2935ce8c925a9c2b23c41aa2a8dd65a31f Mon Sep 17 00:00:00 2001 From: Su_Laus Date: Mon, 6 Feb 2023 21:23:13 +0100 Subject: test_ifd_loop_detection: fix Coverity Scan issue CID 1520750: Null pointer dereferences (NULL_RETURNS) line 55. --- test/test_ifd_loop_detection.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/test_ifd_loop_detection.c b/test/test_ifd_loop_detection.c index 99a749e5..02421f66 100644 --- a/test/test_ifd_loop_detection.c +++ b/test/test_ifd_loop_detection.c @@ -36,30 +36,34 @@ #include "tiffio.h" +/* Compare 'requested_dir_number' with number written in PageName tag + * into the IFD to identify that IFD. */ int is_requested_directory(TIFF *tif, int requested_dir_number, const char *filename) { - char *ptr; + char *ptr = NULL; + char *auxStr = NULL; if (!TIFFGetField(tif, TIFFTAG_PAGENAME, &ptr)) { fprintf(stderr, "Can't get TIFFTAG_PAGENAME tag.\n"); return 0; } - /* Retrieve directory number from ASCII string */ - char *auxStr = strchr(ptr, ' '); - int nthIFD; - nthIFD = atoi(ptr); - /* Check for reading errors */ - if (strncmp(auxStr, " th.", 4)) + if (ptr != NULL) + auxStr = strchr(ptr, ' '); + + if (ptr == NULL || auxStr == NULL || strncmp(auxStr, " th.", 4)) { + ptr = ptr == NULL ? "(null)" : ptr; fprintf(stderr, "Error reading IFD directory number from PageName tag: %s\n", ptr); return 0; } + /* Retrieve IFD identification number from ASCII string */ + const int nthIFD = atoi(ptr); if (nthIFD == requested_dir_number) { return 1; -- cgit v1.2.1