From dcd5ebdcbebcf1f66f65932408683166812ea504 Mon Sep 17 00:00:00 2001 From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Thu, 17 Nov 2022 10:23:26 -0500 Subject: [PATCH] Use uid_t to deal with uids instead of integers We can also use strtoul to convert strings since uid_t is unsigned. --- cups/encode.c | 2 +- scheduler/conf.c | 52 +++++++++++++++++++--------------------- scheduler/cups-deviced.c | 6 ++--- scheduler/cups-exec.c | 8 +++---- scheduler/file.c | 2 +- scheduler/ipp.c | 7 +++--- scheduler/main.c | 11 ++++----- scheduler/policy.c | 2 +- scheduler/printers.c | 21 +++++++++------- scheduler/util.c | 6 ++--- 10 files changed, 58 insertions(+), 59 deletions(-) diff --git a/cups/encode.c b/cups/encode.c index 15b1c6b405..7f0c1d641e 100644 --- a/cups/encode.c +++ b/cups/encode.c @@ -592,7 +592,7 @@ _cupsEncodeOption( if (*s == '-') { if (s[1]) - upper = (int)strtol(s + 1, NULL, 10); + upper = atoi(s + 1); else upper = 2147483647; } diff --git a/scheduler/conf.c b/scheduler/conf.c index 354cac6e00..8467328a55 100644 --- a/scheduler/conf.c +++ b/scheduler/conf.c @@ -1866,8 +1866,7 @@ get_addr_and_mask(const char *value, /* I - String from config file */ * Merge everything into a 32-bit IPv4 address in ip[3]... */ - ip[3] = ((((((unsigned)val[0] << 8) | (unsigned)val[1]) << 8) | - (unsigned)val[2]) << 8) | (unsigned)val[3]; + ip[3] = (val[0] << 24) | (val[1] << 16) | (val[2] << 8) | val[3]; if (ipcount < 4) mask[3] = (0xffffffff << (32 - 8 * ipcount)) & 0xffffffff; @@ -2682,7 +2681,7 @@ parse_variable( char temp[1024]; /* Temporary string */ - for (i = num_vars, var = vars; i > 0; i --, var ++) + for (i = num_vars, var = vars; i; i --, var ++) if (!_cups_strcasecmp(line, var->name)) break; @@ -2724,7 +2723,7 @@ parse_variable( int n; /* Number */ char *units; /* Units */ - n = strtol(value, &units, 0); + n = (int)strtol(value, &units, 0); if (units && *units) { @@ -2777,7 +2776,7 @@ parse_variable( } else { - int n = strtol(value, NULL, 8); + int n = (int)strtol(value, NULL, 8); /* Permissions value */ if (n < 0) @@ -2944,7 +2943,6 @@ read_cupsd_conf(cups_file_t *fp) /* I - File to read from */ temp[HTTP_MAX_BUFFER], /* Temporary buffer for value */ *value; /* Pointer to value */ - int valuelen; /* Length of value */ http_addrlist_t *addrlist, /* Address list */ *addr; /* Current address */ @@ -3377,17 +3375,16 @@ read_cupsd_conf(cups_file_t *fp) /* I - File to read from */ if (!ServerAlias) ServerAlias = cupsArrayNew(NULL, NULL); - for (; *value;) + while (*value) { + size_t valuelen; /* Length of value */ + for (valuelen = 0; value[valuelen]; valuelen ++) if (_cups_isspace(value[valuelen]) || value[valuelen] == ',') - break; - - if (value[valuelen]) - { - value[valuelen] = '\0'; - valuelen ++; - } + { + value[valuelen ++] = '\0'; + break; + } cupsdAddAlias(ServerAlias, value); @@ -3446,8 +3443,8 @@ read_cupsd_conf(cups_file_t *fp) /* I - File to read from */ static int /* O - 1 on success, 0 on failure */ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ { - int i, /* Looping var */ - linenum; /* Current line number */ + size_t i; /* Looping var */ + int linenum; /* Current line number */ char line[HTTP_MAX_BUFFER], /* Line from file */ *value; /* Value from line */ struct group *group; /* Group */ @@ -3508,7 +3505,7 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ */ if (isdigit(value[0])) - Group = (gid_t)atoi(value); + Group = (gid_t)strtoul(value, NULL, 10); else { endgrent(); @@ -3533,7 +3530,7 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ */ if (isdigit(value[0])) - LogFileGroup = (gid_t)atoi(value); + LogFileGroup = (gid_t)strtoul(value, NULL, 10); else { endgrent(); @@ -3557,9 +3554,9 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ * PassEnv variable [... variable] */ - int valuelen; /* Length of variable name */ + size_t valuelen; /* Length of variable name */ - for (; *value;) + while (*value) { for (valuelen = 0; value[valuelen]; valuelen ++) if (_cups_isspace(value[valuelen]) || value[valuelen] == ',') @@ -3567,11 +3564,10 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ if (value[valuelen]) { - value[valuelen] = '\0'; - valuelen ++; + value[valuelen ++] = '\0'; } - for (i = 0; i < (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0])); i ++) + for (i = 0; i < (sizeof(prohibited_env) / sizeof(prohibited_env[0])); i ++) { if (!strcmp(value, prohibited_env[i])) { @@ -3584,7 +3580,7 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ } } - if (i >= (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0]))) + if (i >= sizeof(prohibited_env) / sizeof(prohibited_env[0])) cupsdSetEnv(value, NULL); for (value += valuelen; *value; value ++) @@ -3653,7 +3649,7 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ while (isspace(*valueptr & 255)) *valueptr++ = '\0'; - for (i = 0; i < (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0])); i ++) + for (i = 0; i < (sizeof(prohibited_env) / sizeof(prohibited_env[0])); i ++) { if (!strcmp(value, prohibited_env[i])) { @@ -3666,7 +3662,7 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ } } - if (i >= (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0]))) + if (i >= (sizeof(prohibited_env) / sizeof(prohibited_env[0]))) cupsdSetEnv(value, valueptr); } else @@ -3694,7 +3690,7 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ if (isdigit(value[0] & 255)) { - int uid = atoi(value); + uid_t uid = (uid_t)strtoul(value, NULL, 10); if (!uid) { @@ -3707,7 +3703,7 @@ read_cups_files_conf(cups_file_t *fp) /* I - File to read from */ return (0); } else - User = (uid_t)atoi(value); + User = uid; } else { diff --git a/scheduler/cups-deviced.c b/scheduler/cups-deviced.c index 14478fd990..44bb58fe48 100644 --- a/scheduler/cups-deviced.c +++ b/scheduler/cups-deviced.c @@ -166,10 +166,10 @@ main(int argc, /* I - Number of command-line args */ return (1); } - normal_user = (uid_t)atoi(argv[4]); - if (normal_user <= 0) + normal_user = (uid_t)strtoul(argv[4], NULL, 10); + if (normal_user == 0) { - fprintf(stderr, "ERROR: [cups-deviced] Bad user %d!\n", normal_user); + fprintf(stderr, "ERROR: [cups-deviced] Bad user %u!\n", (unsigned)normal_user); return (1); } diff --git a/scheduler/cups-exec.c b/scheduler/cups-exec.c index 8d51d5279a..8626c704a1 100644 --- a/scheduler/cups-exec.c +++ b/scheduler/cups-exec.c @@ -73,7 +73,7 @@ main(int argc, /* I - Number of command-line args */ if (i >= argc) usage(); - gid = (gid_t)atoi(argv[i]); + gid = (gid_t)strtoul(argv[i], NULL, 10); break; case 'n' : /* -n nice-value */ @@ -89,7 +89,7 @@ main(int argc, /* I - Number of command-line args */ if (i >= argc) usage(); - uid = (uid_t)atoi(argv[i]); + uid = (uid_t)strtoul(argv[i], NULL, 10); break; default : @@ -154,7 +154,7 @@ main(int argc, /* I - Number of command-line args */ { cups_file_t *fp; /* File */ char line[1024]; /* Line from file */ - int linenum = 0; /* Line number in file */ + unsigned linenum = 0; /* Line number in file */ fprintf(stderr, "DEBUG: sandbox_init failed: %s (%s)\n", sandbox_error, strerror(errno)); @@ -165,7 +165,7 @@ main(int argc, /* I - Number of command-line args */ while (cupsFileGets(fp, line, sizeof(line))) { linenum ++; - fprintf(stderr, "DEBUG: %4d %s\n", linenum, line); + fprintf(stderr, "DEBUG: %4u %s\n", linenum, line); } cupsFileClose(fp); } diff --git a/scheduler/file.c b/scheduler/file.c index a2f723a99a..af59ff3c76 100644 --- a/scheduler/file.c +++ b/scheduler/file.c @@ -315,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 */ - int i; /* Looping var */ + size_t i; /* Looping var */ /* diff --git a/scheduler/ipp.c b/scheduler/ipp.c index f468a350ec..c6df281a92 100644 --- a/scheduler/ipp.c +++ b/scheduler/ipp.c @@ -5594,7 +5594,7 @@ create_local_printer( if (nameptr) { - int host_len, + size_t host_len, server_name_len; /* Get host name of device URI */ @@ -7874,13 +7874,14 @@ get_subscriptions(cupsd_client_t *con, /* I - Client connection */ } else if (!strncmp(resource, "/jobs/", 6) && resource[6]) { + int job_id = atoi(resource + 6); printer = NULL; - job = cupsdFindJob(atoi(resource + 6)); + job = cupsdFindJob(job_id); if (!job) { send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), - atoi(resource + 6)); + job_id); return; } } diff --git a/scheduler/main.c b/scheduler/main.c index 65030ba8e4..19ebd341d1 100644 --- a/scheduler/main.c +++ b/scheduler/main.c @@ -1400,7 +1400,7 @@ process_children(void) int pid, /* Process ID of child */ job_id; /* Job ID of child */ cupsd_job_t *job; /* Current job */ - int i; /* Looping var */ + size_t i; /* Looping var */ char name[1024]; /* Process name */ const char *type; /* Type of program */ @@ -1435,8 +1435,7 @@ process_children(void) * Delete certificates for CGI processes... */ - if (pid) - cupsdDeleteCert(pid); + cupsdDeleteCert(pid); /* * Handle completed job filters... @@ -1625,7 +1624,7 @@ process_children(void) * If wait*() is interrupted by a signal, tell main() to call us again... */ - if (pid < 0 && errno == EINTR) + if (pid && errno == EINTR) dead_children = 1; } @@ -2023,7 +2022,7 @@ service_checkin(void) cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: UPSTART_FDS=%s", e); - fd = (int)strtol(e, NULL, 10); + fd = atoi(e); if (fd < 0) { cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Could not parse UPSTART_FDS: %s", strerror(errno)); @@ -2031,7 +2030,7 @@ service_checkin(void) } /* - * Upstart only supportst a single on-demand socket file descriptor... + * Upstart only supports a single on-demand socket file descriptor... */ service_add_listener(fd, 0); diff --git a/scheduler/policy.c b/scheduler/policy.c index b55c16a56b..20c91c8350 100644 --- a/scheduler/policy.c +++ b/scheduler/policy.c @@ -270,7 +270,7 @@ cupsdGetPrivateAttrs( #ifdef DEBUG cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdGetPrivateAttrs(policy=%p(%s), con=%p(%d), " - "printer=%p(%s), owner=\"%s\")", policy, policy->name, con, + "printer=%p(%s), owner=\"%s\")", policy, policy ? policy->name : "", con, con->number, printer, printer ? printer->name : "", owner); #endif /* DEBUG */ diff --git a/scheduler/printers.c b/scheduler/printers.c index 3ca91fd455..28b8f679bd 100644 --- a/scheduler/printers.c +++ b/scheduler/printers.c @@ -1126,7 +1126,7 @@ cupsdLoadAllPrinters(void) */ if (value) - p->state_time = atoi(value); + p->state_time = (time_t)strtol(value, NULL, 10); } else if (!_cups_strcasecmp(line, "ConfigTime")) { @@ -1135,7 +1135,7 @@ cupsdLoadAllPrinters(void) */ if (value) - p->config_time = atoi(value); + p->config_time = (time_t)strtol(value, NULL, 10); } else if (!_cups_strcasecmp(line, "Accepting")) { @@ -1160,7 +1160,7 @@ cupsdLoadAllPrinters(void) else if (!_cups_strcasecmp(line, "Type")) { if (value) - p->type = (cups_ptype_t)atoi(value); + p->type = (cups_ptype_t)strtoul(value, NULL, 10); else cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d of printers.conf.", linenum); @@ -1314,7 +1314,7 @@ cupsdLoadAllPrinters(void) cupsdSetPrinterAttrs(p); if (!strcmp(value, "marker-change-time")) - p->marker_time = atoi(valueptr); + p->marker_time = (time_t)strtol(valueptr, NULL, 10); else cupsdSetPrinterAttr(p, value, valueptr); } @@ -2018,7 +2018,7 @@ cupsdSetPrinterAttr( if ((ptr = strchr(start, ',')) != NULL) *ptr++ = '\0'; - attr->values[i].integer = strtol(start, NULL, 10); + attr->values[i].integer = atoi(start); if (ptr) start = ptr; @@ -3451,9 +3451,10 @@ add_printer_filter( return; } - ptr ++; - while (_cups_isspace(*ptr)) + do + { ptr ++; + } while (_cups_isspace(*ptr)); _cups_strcpy(program, ptr); } @@ -5010,9 +5011,11 @@ load_ppd(cupsd_printer_t *p) /* I - Printer */ sourceRef = CGImageSourceCreateWithURL(icnsFileUrl, NULL); if (sourceRef) { - for (i = 0; i < (int)CGImageSourceGetCount(sourceRef); i ++) + size_t index; + const size_t count = CGImageSourceGetCount(sourceRef); + for (index = 0; index < count; index ++) { - imageRef = CGImageSourceCreateImageAtIndex(sourceRef, (size_t)i, NULL); + imageRef = CGImageSourceCreateImageAtIndex(sourceRef, index, NULL); if (!imageRef) continue; diff --git a/scheduler/util.c b/scheduler/util.c index 228733f388..655a07eb34 100644 --- a/scheduler/util.c +++ b/scheduler/util.c @@ -161,11 +161,11 @@ cupsdExec(const char *command, /* I - Full path to program */ char **argv) /* I - Command-line arguments */ { #ifdef __APPLE__ - int i, j; /* Looping vars */ + size_t i, j; /* Looping vars */ char *envp[500], /* Array of environment variables */ cfprocesspath[1024], /* CFProcessPath environment variable */ linkpath[1024]; /* Link path for symlinks... */ - int linkbytes; /* Bytes for link path */ + ssize_t linkbytes; /* Bytes for link path */ /* @@ -201,7 +201,7 @@ cupsdExec(const char *command, /* I - Full path to program */ */ for (i = 1, j = 0; - environ[j] && i < (int)(sizeof(envp) / sizeof(envp[0]) - 1); + environ[j] && i < (sizeof(envp) / sizeof(envp[0]) - 1); j ++) if (strncmp(environ[j], "CFProcessPath=", 14)) envp[i ++] = environ[j]; -- 2.47.2