Remove superfluous casts

-  Every non-const pointer converts automatically to void *.
-  Every pointer converts automatically to void *.
-  void * converts to any other pointer.
-  const void * converts to any other const pointer.
-  Integer variables convert to each other.

I changed the declaration of a few variables in order to allow removing
a cast.

However, I didn't attempt to edit casts inside comparisons, since they
are very delicate.  I also kept casts in variadic functions, since they
are necessary, and in allocation functions, because I have other plans
for them.

I also changed a few casts to int that are better as ptrdiff_t.

This change has triggered some warnings about const correctness issues,
which have also been fixed in this patch (see for example src/login.c).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-02-01 13:50:48 +01:00
committed by Serge Hallyn
parent 66daa74232
commit bddcd9b095
62 changed files with 260 additions and 319 deletions

View File

@@ -100,7 +100,7 @@ int add_groups (const char *list)
}
if (added) {
ret = setgroups ((size_t)ngroups, grouplist);
ret = setgroups (ngroups, grouplist);
free (grouplist);
return ret;
}

View File

@@ -139,7 +139,7 @@ int expire (const struct passwd *pw, /*@null@*/const struct spwd *sp)
void agecheck (/*@null@*/const struct spwd *sp)
{
long now = (long) time(NULL) / SCALE;
long now = time(NULL) / SCALE;
long remain;
if (NULL == sp) {

View File

@@ -62,7 +62,7 @@ void audit_logger (int type, unused const char *pgname, const char *op,
return;
} else {
audit_log_acct_message (audit_fd, type, NULL, op, name, id,
NULL, NULL, NULL, (int) result);
NULL, NULL, NULL, result);
}
}
@@ -77,7 +77,7 @@ void audit_logger_message (const char *message, shadow_audit_result result)
NULL, /* hostname */
NULL, /* addr */
NULL, /* tty */
(int) result);
result);
}
}

View File

@@ -51,7 +51,7 @@ void chown_tty (const struct passwd *info)
*/
if ( (fchown (STDIN_FILENO, info->pw_uid, gid) != 0)
|| (fchmod (STDIN_FILENO, (mode_t)getdef_num ("TTYPERM", 0600)) != 0)) {
|| (fchmod (STDIN_FILENO, getdef_num ("TTYPERM", 0600)) != 0)) {
int err = errno;
FILE *shadow_logfd = log_get_logfd();

View File

@@ -22,7 +22,7 @@
*/
void cleanup_report_add_group (void *group_name)
{
const char *name = (const char *)group_name;
const char *name = group_name;
SYSLOG ((LOG_ERR, "failed to add group %s", name));
#ifdef WITH_AUDIT
@@ -40,7 +40,7 @@ void cleanup_report_add_group (void *group_name)
*/
void cleanup_report_del_group (void *group_name)
{
const char *name = (const char *)group_name;
const char *name = group_name;
SYSLOG ((LOG_ERR, "failed to remove group %s", name));
#ifdef WITH_AUDIT
@@ -95,7 +95,7 @@ void cleanup_report_mod_gshadow (void *cleanup_info)
*/
void cleanup_report_add_group_group (void *group_name)
{
const char *name = (const char *)group_name;
const char *name = group_name;
SYSLOG ((LOG_ERR, "failed to add group %s to %s", name, gr_dbname ()));
#ifdef WITH_AUDIT
@@ -115,7 +115,7 @@ void cleanup_report_add_group_group (void *group_name)
*/
void cleanup_report_add_group_gshadow (void *group_name)
{
const char *name = (const char *)group_name;
const char *name = group_name;
SYSLOG ((LOG_ERR, "failed to add group %s to %s", name, sgr_dbname ()));
#ifdef WITH_AUDIT
@@ -136,7 +136,7 @@ void cleanup_report_add_group_gshadow (void *group_name)
*/
void cleanup_report_del_group_group (void *group_name)
{
const char *name = (const char *)group_name;
const char *name = group_name;
SYSLOG ((LOG_ERR,
"failed to remove group %s from %s",
@@ -159,7 +159,7 @@ void cleanup_report_del_group_group (void *group_name)
*/
void cleanup_report_del_group_gshadow (void *group_name)
{
const char *name = (const char *)group_name;
const char *name = group_name;
SYSLOG ((LOG_ERR,
"failed to remove group %s from %s",

View File

@@ -22,7 +22,7 @@
*/
void cleanup_report_add_user (void *user_name)
{
const char *name = (const char *)user_name;
const char *name = user_name;
SYSLOG ((LOG_ERR, "failed to add user %s", name));
#ifdef WITH_AUDIT
@@ -59,7 +59,7 @@ void cleanup_report_mod_passwd (void *cleanup_info)
*/
void cleanup_report_add_user_passwd (void *user_name)
{
const char *name = (const char *)user_name;
const char *name = user_name;
SYSLOG ((LOG_ERR, "failed to add user %s to %s", name, pw_dbname ()));
#ifdef WITH_AUDIT
@@ -79,7 +79,7 @@ void cleanup_report_add_user_passwd (void *user_name)
*/
void cleanup_report_add_user_shadow (void *user_name)
{
const char *name = (const char *)user_name;
const char *name = user_name;
SYSLOG ((LOG_ERR, "failed to add user %s to %s", name, spw_dbname ()));
#ifdef WITH_AUDIT

View File

@@ -70,7 +70,7 @@ static bool is_listed (const char *cfgin, const char *tty, bool def)
* See if this tty is listed in the console file.
*/
while (fgets (buf, (int) sizeof (buf), fp) != NULL) {
while (fgets (buf, sizeof (buf), fp) != NULL) {
/* Remove optional trailing '\n'. */
buf[strcspn (buf, "\n")] = '\0';
if (strcmp (buf, tty) == 0) {

View File

@@ -768,7 +768,7 @@ static ssize_t full_write(int fd, const void *buf, size_t count) {
written += res;
buf = (const unsigned char*)buf + res;
count -= (size_t)res;
count -= res;
}
return written;
@@ -850,7 +850,7 @@ static int copy_file (const struct path_info *src, const struct path_info *dst,
break;
}
if (full_write (ofd, buf, (size_t)cnt) < 0) {
if (full_write (ofd, buf, cnt) < 0) {
(void) close (ofd);
(void) close (ifd);
return -1;

View File

@@ -53,7 +53,7 @@ void failure (uid_t uid, const char *tty, struct faillog *fl)
*/
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (read (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl)) {
|| (read (fd, fl, sizeof *fl) != (ssize_t) sizeof *fl)) {
/* This is not necessarily a failure. The file is
* initially zero length.
*
@@ -86,7 +86,7 @@ void failure (uid_t uid, const char *tty, struct faillog *fl)
*/
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (write (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl)
|| (write (fd, fl, sizeof *fl) != (ssize_t) sizeof *fl)
|| (close (fd) != 0)) {
SYSLOG ((LOG_WARN,
"Can't write faillog entry for UID %lu in %s.",
@@ -163,7 +163,7 @@ int failcheck (uid_t uid, struct faillog *fl, bool failed)
*/
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (read (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl)) {
|| (read (fd, fl, sizeof *fl) != (ssize_t) sizeof *fl)) {
(void) close (fd);
return 1;
}
@@ -185,7 +185,7 @@ int failcheck (uid_t uid, struct faillog *fl, bool failed)
fail.fail_cnt = 0;
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (write (fd, (const void *) &fail, sizeof fail) != (ssize_t) sizeof fail)
|| (write (fd, &fail, sizeof fail) != (ssize_t) sizeof fail)
|| (close (fd) != 0)) {
SYSLOG ((LOG_WARN,
"Can't reset faillog entry for UID %lu in %s.",
@@ -278,7 +278,7 @@ void failtmp (const char *username, const struct utmp *failent)
* Append the new failure record and close the log file.
*/
if ( (write (fd, (const void *) failent, sizeof *failent) != (ssize_t) sizeof *failent)
if ( (write (fd, failent, sizeof *failent) != (ssize_t) sizeof *failent)
|| (close (fd) != 0)) {
SYSLOG ((LOG_WARN,
"Can't append failure of user %s to %s.",

View File

@@ -40,15 +40,14 @@ static int get_ranges (bool sys_group, gid_t *min_id, gid_t *max_id,
*preferred_min = (gid_t) 1;
/* Get the minimum ID range from login.defs or default to 101 */
*min_id = (gid_t) getdef_ulong ("SYS_GID_MIN", 101UL);
*min_id = getdef_ulong ("SYS_GID_MIN", 101UL);
/*
* If SYS_GID_MAX is unspecified, we should assume it to be one
* less than the GID_MIN (which is reserved for non-system accounts)
*/
gid_def_max = (gid_t) getdef_ulong ("GID_MIN", 1000UL) - 1;
*max_id = (gid_t) getdef_ulong ("SYS_GID_MAX",
(unsigned long) gid_def_max);
gid_def_max = getdef_ulong ("GID_MIN", 1000UL) - 1;
*max_id = getdef_ulong ("SYS_GID_MAX", gid_def_max);
/* Check that the ranges make sense */
if (*max_id < *min_id) {
@@ -71,8 +70,8 @@ static int get_ranges (bool sys_group, gid_t *min_id, gid_t *max_id,
/* Non-system groups */
/* Get the values from login.defs or use reasonable defaults */
*min_id = (gid_t) getdef_ulong ("GID_MIN", 1000UL);
*max_id = (gid_t) getdef_ulong ("GID_MAX", 60000UL);
*min_id = getdef_ulong ("GID_MIN", 1000UL);
*max_id = getdef_ulong ("GID_MAX", 60000UL);
/*
* The preferred minimum should match the standard ID minimum

View File

@@ -40,15 +40,14 @@ static int get_ranges (bool sys_user, uid_t *min_id, uid_t *max_id,
*preferred_min = (uid_t) 1;
/* Get the minimum ID range from login.defs or default to 101 */
*min_id = (uid_t) getdef_ulong ("SYS_UID_MIN", 101UL);
*min_id = getdef_ulong ("SYS_UID_MIN", 101UL);
/*
* If SYS_UID_MAX is unspecified, we should assume it to be one
* less than the UID_MIN (which is reserved for non-system accounts)
*/
uid_def_max = (uid_t) getdef_ulong ("UID_MIN", 1000UL) - 1;
*max_id = (uid_t) getdef_ulong ("SYS_UID_MAX",
(unsigned long) uid_def_max);
uid_def_max = getdef_ulong ("UID_MIN", 1000UL) - 1;
*max_id = getdef_ulong ("SYS_UID_MAX", uid_def_max);
/* Check that the ranges make sense */
if (*max_id < *min_id) {
@@ -71,8 +70,8 @@ static int get_ranges (bool sys_user, uid_t *min_id, uid_t *max_id,
/* Non-system users */
/* Get the values from login.defs or use reasonable defaults */
*min_id = (uid_t) getdef_ulong ("UID_MIN", 1000UL);
*max_id = (uid_t) getdef_ulong ("UID_MAX", 60000UL);
*min_id = getdef_ulong ("UID_MIN", 1000UL);
*max_id = getdef_ulong ("UID_MAX", 60000UL);
/*
* The preferred minimum should match the standard ID minimum

View File

@@ -36,7 +36,7 @@ extern /*@only@*//*@null@*/struct group *getgr_nam_gid (/*@null@*/const char *gr
&& ('\0' == *endptr)
&& (ERANGE != errno)
&& (/*@+longintegral@*/gid == (gid_t)gid)/*@=longintegral@*/) {
return xgetgrgid ((gid_t) gid);
return xgetgrgid (gid);
}
return xgetgrnam (grname);
}

View File

@@ -61,7 +61,7 @@
fallback, epoch);
} else {
/* Valid */
return (time_t)epoch;
return epoch;
}
return fallback;

View File

@@ -66,7 +66,7 @@ bool hushed (const char *username)
if (NULL == fp) {
return false;
}
for (found = false; !found && (fgets (buf, (int) sizeof buf, fp) == buf);) {
for (found = false; !found && (fgets (buf, sizeof buf, fp) == buf);) {
buf[strcspn (buf, "\n")] = '\0';
found = (strcmp (buf, pw->pw_shell) == 0) ||
(strcmp (buf, pw->pw_name) == 0);

View File

@@ -40,7 +40,7 @@ int isexpired (const struct passwd *pw, /*@null@*/const struct spwd *sp)
{
long now;
now = (long) time(NULL) / SCALE;
now = time(NULL) / SCALE;
if (NULL == sp) {
return 0;

View File

@@ -68,7 +68,7 @@ static int setrlimit_value (unsigned int resource,
return 0;
}
longlimit *= multiplier;
limit = (rlim_t)longlimit;
limit = longlimit;
if (longlimit != limit)
{
/* FIXME: Again, silent error handling...
@@ -95,7 +95,7 @@ static int set_prio (const char *value)
|| (prio != (int) prio)) {
return 0;
}
if (setpriority (PRIO_PROCESS, 0, (int) prio) != 0) {
if (setpriority (PRIO_PROCESS, 0, prio) != 0) {
return LOGIN_ERROR_RLIMIT;
}
return 0;
@@ -111,7 +111,7 @@ static int set_umask (const char *value)
return 0;
}
(void) umask ((mode_t) mask);
(void) umask (mask);
return 0;
}
@@ -508,7 +508,7 @@ void setup_limits (const struct passwd *info)
if ( (getlong (cp + 4, &inc) == 1)
&& (inc >= -20) && (inc <= 20)) {
errno = 0;
if ( (nice ((int) inc) != -1)
if ( (nice (inc) != -1)
|| (0 != errno)) {
continue;
}
@@ -525,7 +525,7 @@ void setup_limits (const struct passwd *info)
long int blocks;
if ( (getlong (cp + 7, &blocks) == 0)
|| (blocks != (int) blocks)
|| (set_filesize_limit ((int) blocks) != 0)) {
|| (set_filesize_limit (blocks) != 0)) {
SYSLOG ((LOG_WARN,
"Can't set the ulimit for user %s",
info->pw_name));
@@ -540,7 +540,7 @@ void setup_limits (const struct passwd *info)
"Can't set umask value for user %s",
info->pw_name));
} else {
(void) umask ((mode_t) mask);
(void) umask (mask);
}
continue;

View File

@@ -82,7 +82,7 @@ void dolastlog (
strncpy (newlog.ll_host, host, sizeof (newlog.ll_host) - 1);
#endif
if ( (lseek (fd, offset, SEEK_SET) != offset)
|| (write (fd, (const void *) &newlog, sizeof newlog) != (ssize_t) sizeof newlog)
|| (write (fd, &newlog, sizeof newlog) != (ssize_t) sizeof newlog)
|| (close (fd) != 0)) {
SYSLOG ((LOG_WARN,
"Can't write lastlog entry for UID %lu in %s.",

View File

@@ -81,7 +81,7 @@ void login_prompt (const char *prompt, char *name, int namesize)
*/
memzero (buf, sizeof buf);
if (fgets (buf, (int) sizeof buf, stdin) != buf) {
if (fgets (buf, sizeof buf, stdin) != buf) {
exit (EXIT_FAILURE);
}

View File

@@ -257,7 +257,7 @@ static /*@observer@*//*@null@*/const char *obscure_msg (
}
}
maxlen = (size_t) getdef_num ("PASS_MAX_LEN", 8);
maxlen = getdef_num ("PASS_MAX_LEN", 8);
if ( (oldlen <= maxlen)
&& (newlen <= maxlen)) {
return NULL;

View File

@@ -43,8 +43,7 @@ static int ni_conv (int num_msg,
return PAM_CONV_ERR;
}
responses = (struct pam_response *) calloc ((size_t) num_msg,
sizeof (*responses));
responses = (struct pam_response *) calloc (num_msg, sizeof (*responses));
if (NULL == responses) {
return PAM_CONV_ERR;
}

View File

@@ -330,7 +330,7 @@ extern struct group *prefix_getgr_nam_gid(const char *grname)
&& ('\0' == *endptr)
&& (ERANGE != errno)
&& (gid == (gid_t)gid)) {
return prefix_getgrgid ((gid_t) gid);
return prefix_getgrgid (gid);
}
g = prefix_getgrnam (grname);
return g ? __gr_dup(g) : NULL;

View File

@@ -42,7 +42,7 @@ struct spwd *pwd_to_spwd (const struct passwd *pw)
*/
sp.sp_min = 0;
sp.sp_max = (10000L * DAY) / SCALE;
sp.sp_lstchg = (long) gettime () / SCALE;
sp.sp_lstchg = gettime () / SCALE;
if (0 == sp.sp_lstchg) {
/* Better disable aging than requiring a password
* change */

View File

@@ -377,26 +377,26 @@ static /*@observer@*/const char *gensalt (size_t salt_size)
} else if (0 == strcmp (method, "BCRYPT")) {
BCRYPTMAGNUM(result);
salt_len = BCRYPT_SALT_SIZE;
rounds = BCRYPT_get_salt_rounds ((int *) arg);
rounds = BCRYPT_get_salt_rounds (arg);
BCRYPT_salt_rounds_to_buf (result, rounds);
#endif /* USE_BCRYPT */
#ifdef USE_YESCRYPT
} else if (0 == strcmp (method, "YESCRYPT")) {
MAGNUM(result, 'y');
salt_len = YESCRYPT_SALT_SIZE;
rounds = YESCRYPT_get_salt_cost ((int *) arg);
rounds = YESCRYPT_get_salt_cost (arg);
YESCRYPT_salt_cost_to_buf (result, rounds);
#endif /* USE_YESCRYPT */
#ifdef USE_SHA_CRYPT
} else if (0 == strcmp (method, "SHA256")) {
MAGNUM(result, '5');
salt_len = SHA_CRYPT_SALT_SIZE;
rounds = SHA_get_salt_rounds ((int *) arg);
rounds = SHA_get_salt_rounds (arg);
SHA_salt_rounds_to_buf (result, rounds);
} else if (0 == strcmp (method, "SHA512")) {
MAGNUM(result, '6');
salt_len = SHA_CRYPT_SALT_SIZE;
rounds = SHA_get_salt_rounds ((int *) arg);
rounds = SHA_get_salt_rounds (arg);
SHA_salt_rounds_to_buf (result, rounds);
#endif /* USE_SHA_CRYPT */
} else if (0 != strcmp (method, "DES")) {

View File

@@ -43,7 +43,7 @@ void ttytype (const char *line)
perror (typefile);
return;
}
while (fgets (buf, (int) sizeof buf, fp) == buf) {
while (fgets (buf, sizeof buf, fp) == buf) {
if (buf[0] == '#') {
continue;
}

View File

@@ -34,7 +34,7 @@
fp = fopen (fname, "r");
if ( (NULL == fp)
|| (fgets (tzbuf, (int) sizeof (tzbuf), fp) == NULL)) {
|| (fgets (tzbuf, sizeof (tzbuf), fp) == NULL)) {
def_tz = getdef_str ("ENV_TZ");
if ((NULL == def_tz) || ('/' == def_tz[0])) {
def_tz = "TZ=CST6CDT";

View File

@@ -115,7 +115,7 @@ static void updwtmp (const char *filename, const struct utmp *ut)
fd = open (filename, O_APPEND | O_WRONLY, 0);
if (fd >= 0) {
write (fd, (const char *) ut, sizeof (*ut));
write (fd, ut, sizeof (*ut));
close (fd);
}
}

View File

@@ -47,7 +47,7 @@ bool yes_or_no (bool read_only)
* Get a line and see what the first character is.
*/
/* TODO: use gettext */
if (fgets (buf, (int) sizeof buf, stdin) == buf) {
if (fgets (buf, sizeof buf, stdin) == buf) {
return buf[0] == 'y' || buf[0] == 'Y';
}