Skip to content

Commit

Permalink
Apply some minor changes suggested by Coverity.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Sep 10, 2024
1 parent 6d33b1f commit 566c19f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
3 changes: 2 additions & 1 deletion cups/ipp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions cups/langprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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);
}
Expand Down
22 changes: 14 additions & 8 deletions ppdc/ppdc-source.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
29 changes: 16 additions & 13 deletions scheduler/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
#ifdef HAVE_REMOVEFILE
# include <removefile.h>
#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 */


Expand Down Expand Up @@ -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 */


/*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */


/*
Expand All @@ -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;
}

/*
Expand Down

0 comments on commit 566c19f

Please sign in to comment.