From 566c19f47fe896990741836a0e52a9d4bd7741b2 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Tue, 10 Sep 2024 11:42:43 -0400 Subject: [PATCH] Apply some minor changes suggested by Coverity. --- cups/ipp.c | 3 ++- cups/langprintf.c | 17 +++++++++++------ ppdc/ppdc-source.cxx | 22 ++++++++++++++-------- scheduler/file.c | 29 ++++++++++++++++------------- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/cups/ipp.c b/cups/ipp.c index 752d177cd2..fdfd21b1c4 100644 --- a/cups/ipp.c +++ b/cups/ipp.c @@ -1636,7 +1636,8 @@ ippDateToTime(const ipp_uchar_t *date) // I - RFC 2579 date info unixdate.tm_min = date[5]; unixdate.tm_sec = date[6]; - t = mktime(&unixdate); + if ((t = mktime(&unixdate)) < 0) + return (0); if (date[8] == '-') t += date[9] * 3600 + date[10] * 60; diff --git a/cups/langprintf.c b/cups/langprintf.c index bacf474a17..ac36e1f946 100644 --- a/cups/langprintf.c +++ b/cups/langprintf.c @@ -209,6 +209,7 @@ _cupsLangPuts(FILE *fp, /* I - File to write to */ const char *message) /* I - Message string to use */ { ssize_t bytes; /* Number of bytes formatted */ + size_t length; /* Formatted message length */ char output[8192]; /* Message buffer */ _cups_globals_t *cg; /* Global data */ @@ -229,18 +230,22 @@ _cupsLangPuts(FILE *fp, /* I - File to write to */ * Transcode to the destination charset... */ - bytes = cupsUTF8ToCharset(output, - (cups_utf8_t *)_cupsLangString(cg->lang_default, - message), - sizeof(output) - 4, cg->lang_default->encoding); - bytes += cupsUTF8ToCharset(output + bytes, (cups_utf8_t *)"\n", (int)(sizeof(output) - (size_t)bytes), cg->lang_default->encoding); + if ((bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)_cupsLangString(cg->lang_default, message), sizeof(output) - 4, cg->lang_default->encoding)) < 0) + return (-1); + + length = (size_t)bytes; + + if ((bytes = cupsUTF8ToCharset(output + bytes, (cups_utf8_t *)"\n", (int)(sizeof(output) - (size_t)bytes), cg->lang_default->encoding)) < 0) + return (-1); + + length += (size_t)bytes; /* * Write the string and return the number of bytes written... */ if (bytes > 0) - return ((int)fwrite(output, 1, (size_t)bytes, fp)); + return ((int)fwrite(output, 1, length, fp)); else return ((int)bytes); } diff --git a/ppdc/ppdc-source.cxx b/ppdc/ppdc-source.cxx index 70b9c73206..d7edb9d48c 100644 --- a/ppdc/ppdc-source.cxx +++ b/ppdc/ppdc-source.cxx @@ -2,8 +2,8 @@ // Source class for the CUPS PPD Compiler. // // Copyright © 2020-2024 by OpenPrinting. -// Copyright 2007-2018 by Apple Inc. -// Copyright 2002-2007 by Easy Software Products. +// Copyright © 2007-2018 by Apple Inc. +// Copyright © 2002-2007 by Easy Software Products. // // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. @@ -2166,7 +2166,8 @@ ppdcSource::quotef(cups_file_t *fp, // I - File to write to ...) // I - Additional args as needed { va_list ap; // Pointer to additional arguments - int bytes; // Bytes written + int bytes, // Bytes written + tbytes; // Temporary bytes written char sign, // Sign of format width size, // Size character (h, l, L) type; // Format type character @@ -2249,7 +2250,8 @@ ppdcSource::quotef(cups_file_t *fp, // I - File to write to memcpy(tformat, bufformat, (size_t)(format - bufformat)); tformat[format - bufformat] = '\0'; - bytes += cupsFilePrintf(fp, tformat, va_arg(ap, double)); + if ((tbytes = cupsFilePrintf(fp, tformat, va_arg(ap, double))) > 0) + bytes += tbytes; break; case 'B' : // Integer formats @@ -2268,13 +2270,16 @@ ppdcSource::quotef(cups_file_t *fp, // I - File to write to # ifdef HAVE_LONG_LONG if (size == 'L') - bytes += cupsFilePrintf(fp, tformat, va_arg(ap, long long)); + tbytes = cupsFilePrintf(fp, tformat, va_arg(ap, long long)); else # endif /* HAVE_LONG_LONG */ if (size == 'l') - bytes += cupsFilePrintf(fp, tformat, va_arg(ap, long)); + tbytes = cupsFilePrintf(fp, tformat, va_arg(ap, long)); else - bytes += cupsFilePrintf(fp, tformat, va_arg(ap, int)); + tbytes = cupsFilePrintf(fp, tformat, va_arg(ap, int)); + + if (tbytes > 0) + bytes += tbytes; break; case 'p' : // Pointer value @@ -2284,7 +2289,8 @@ ppdcSource::quotef(cups_file_t *fp, // I - File to write to memcpy(tformat, bufformat, (size_t)(format - bufformat)); tformat[format - bufformat] = '\0'; - bytes += cupsFilePrintf(fp, tformat, va_arg(ap, void *)); + if ((tbytes = cupsFilePrintf(fp, tformat, va_arg(ap, void *))) > 0) + bytes += tbytes; break; case 'c' : // Character or character array diff --git a/scheduler/file.c b/scheduler/file.c index e0a5a45401..62c3843839 100644 --- a/scheduler/file.c +++ b/scheduler/file.c @@ -19,8 +19,7 @@ #ifdef HAVE_REMOVEFILE # include #else -static int overwrite_data(int fd, const char *buffer, int bufsize, - int filesize); +static int overwrite_data(int fd, const char *buffer, size_t bufsize, off_t filesize); #endif /* HAVE_REMOVEFILE */ @@ -316,7 +315,7 @@ cupsdRemoveFile(const char *filename) /* I - File to remove */ int fd; /* File descriptor */ struct stat info; /* File information */ char buffer[512]; /* Data buffer */ - size_t i; /* Looping var */ + size_t i; /* Looping var */ /* @@ -363,8 +362,8 @@ cupsdRemoveFile(const char *filename) /* I - File to remove */ CUPS_SRAND(time(NULL)); for (i = 0; i < sizeof(buffer); i ++) - buffer[i] = CUPS_RAND(); - if (overwrite_data(fd, buffer, sizeof(buffer), (int)info.st_size)) + buffer[i] = (char)CUPS_RAND(); + if (overwrite_data(fd, buffer, sizeof(buffer), info.st_size)) { close(fd); return (-1); @@ -403,10 +402,11 @@ cupsdUnlinkOrRemoveFile( static int /* O - 0 on success, -1 on error */ overwrite_data(int fd, /* I - File descriptor */ const char *buffer, /* I - Buffer to write */ - int bufsize, /* I - Size of buffer */ - int filesize) /* I - Size of file */ + size_t bufsize, /* I - Size of buffer */ + off_t filesize) /* I - Size of file */ { - int bytes; /* Bytes to write/written */ + size_t wbytes; /* Bytes to write */ + ssize_t written; /* Bytes written */ /* @@ -422,15 +422,18 @@ overwrite_data(int fd, /* I - File descriptor */ while (filesize > 0) { - if (filesize > bufsize) - bytes = bufsize; + if (filesize > (off_t)bufsize) + wbytes = bufsize; else - bytes = filesize; + wbytes = (size_t)filesize; - if ((bytes = write(fd, buffer, (size_t)bytes)) < 0) + if ((written = write(fd, buffer, wbytes)) < 0) return (-1); - filesize -= bytes; + if ((off_t)written > filesize) + filesize = 0; + else + filesize -= (off_t)written; } /*