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

@@ -216,8 +216,8 @@ int do_fcntl_lock (const char *file, bool log, short type)
fd = open (file, O_WRONLY, 0600);
if (-1 == fd) {
if (log) {
(void) fprintf (shadow_logfd, "%s: %s: %s\n",
shadow_progname, file, strerror (errno));
(void) fprintf (shadow_logfd, "%s: %s: %s\n",
shadow_progname, file, strerror (errno));
}
return 0;
}
@@ -518,7 +518,7 @@ int commonio_open (struct commonio_db *db, int mode)
goto cleanup_ENOMEM;
}
while (db->ops->fgets (buf, (int) buflen, db->fp) == buf) {
while (db->ops->fgets (buf, buflen, db->fp) == buf) {
while ( ((cp = strrchr (buf, '\n')) == NULL)
&& (feof (db->fp) == 0)) {
size_t len;

View File

@@ -74,7 +74,7 @@ void change_field (char *buf, size_t maxsize, const char *prompt)
printf ("\t%s [%s]: ", prompt, buf);
(void) fflush (stdout);
if (fgets (newf, (int) maxsize, stdin) != newf) {
if (fgets (newf, maxsize, stdin) != newf) {
return;
}

View File

@@ -25,7 +25,7 @@ int get_gid (const char *gidstr, gid_t *gid)
return 0;
}
*gid = (gid_t)val;
*gid = val;
return 1;
}

View File

@@ -25,7 +25,7 @@ int get_pid (const char *pidstr, pid_t *pid)
return 0;
}
*pid = (pid_t)val;
*pid = val;
return 1;
}

View File

@@ -25,7 +25,7 @@ int get_uid (const char *uidstr, uid_t *uid)
return 0;
}
*uid = (uid_t)val;
*uid = val;
return 1;
}

View File

@@ -13,6 +13,7 @@
#include "prototypes.h"
#include "defines.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
@@ -191,7 +192,7 @@ static void def_load (void);
}
d = def_find (item);
return ((NULL == d)? (const char *) NULL : d->value);
return (NULL == d) ? NULL : d->value;
}
@@ -249,7 +250,7 @@ int getdef_num (const char *item, int dflt)
return dflt;
}
return (int) val;
return val;
}
@@ -284,7 +285,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt)
return dflt;
}
return (unsigned int) val;
return val;
}
@@ -428,7 +429,7 @@ static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name)
SYSLOG ((LOG_CRIT, "unknown configuration item `%s'", name));
out:
return (struct itemdef *) NULL;
return NULL;
}
/*
@@ -540,12 +541,12 @@ static void def_load (void)
/*
* Go through all of the lines in the file.
*/
while (fgets (buf, (int) sizeof (buf), fp) != NULL) {
while (fgets (buf, sizeof (buf), fp) != NULL) {
/*
* Trim trailing whitespace.
*/
for (i = (int) strlen (buf) - 1; i >= 0; --i) {
for (i = (ptrdiff_t) strlen (buf) - 1; i >= 0; --i) {
if (!isspace (buf[i])) {
break;
}

View File

@@ -159,7 +159,7 @@ int gr_open (int mode)
int gr_update (const struct group *gr)
{
return commonio_update (&group_db, (const void *) gr);
return commonio_update (&group_db, gr);
}
int gr_remove (const char *name)
@@ -247,8 +247,8 @@ static int group_open_hook (void)
for (gr1 = group_db.head; NULL != gr1; gr1 = gr1->next) {
for (gr2 = gr1->next; NULL != gr2; gr2 = gr2->next) {
struct group *g1 = (struct group *)gr1->eptr;
struct group *g2 = (struct group *)gr2->eptr;
struct group *g1 = gr1->eptr;
struct group *g2 = gr2->eptr;
if (NULL != g1 &&
NULL != g2 &&
0 == strcmp (g1->gr_name, g2->gr_name) &&
@@ -302,8 +302,8 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
return NULL;
}
gptr1 = (struct group *)gr1->eptr;
gptr2 = (struct group *)gr2->eptr;
gptr1 = gr1->eptr;
gptr2 = gr2->eptr;
if (NULL == gptr2 || NULL == gptr1) {
errno = EINVAL;
return NULL;
@@ -377,7 +377,7 @@ static int split_groups (unsigned int max_members)
struct commonio_entry *gr;
for (gr = group_db.head; NULL != gr; gr = gr->next) {
struct group *gptr = (struct group *)gr->eptr;
struct group *gptr = gr->eptr;
struct commonio_entry *new;
struct group *new_gptr;
unsigned int members = 0;
@@ -406,7 +406,7 @@ static int split_groups (unsigned int max_members)
errno = ENOMEM;
return 0;
}
new_gptr = (struct group *)new->eptr;
new_gptr = new->eptr;
new->line = NULL;
new->changed = true;

View File

@@ -207,9 +207,9 @@ void endsgent (void)
}
#ifdef USE_NIS
while (fgetsx (buf, (int) buflen, fp) == buf)
while (fgetsx (buf, buflen, fp) == buf)
#else
if (fgetsx (buf, (int) buflen, fp) == buf)
if (fgetsx (buf, buflen, fp) == buf)
#endif
{
while ( ((cp = strrchr (buf, '\n')) == NULL)

View File

@@ -134,7 +134,7 @@ static struct port *getportent (void)
* Lines which begin with '#' are all ignored.
*/
if (fgets (buf, (int) sizeof buf, ports) == 0) {
if (fgets (buf, sizeof buf, ports) == 0) {
errno = saveerr;
return 0;
}

View File

@@ -137,7 +137,7 @@ int pw_open (int mode)
int pw_update (const struct passwd *pw)
{
return commonio_update (&passwd_db, (const void *) pw);
return commonio_update (&passwd_db, pw);
}
int pw_remove (const char *name)

View File

@@ -253,7 +253,7 @@ int sgr_open (int mode)
int sgr_update (const struct sgrp *sg)
{
return commonio_update (&gshadow_db, (const void *) sg);
return commonio_update (&gshadow_db, sg);
}
int sgr_remove (const char *name)

View File

@@ -336,9 +336,9 @@ struct spwd *fgetspent (FILE * fp)
}
#ifdef USE_NIS
while (fgets (buf, (int) sizeof buf, fp) != NULL)
while (fgets (buf, sizeof buf, fp) != NULL)
#else
if (fgets (buf, (int) sizeof buf, fp) != NULL)
if (fgets (buf, sizeof buf, fp) != NULL)
#endif
{
cp = strchr (buf, '\n');

View File

@@ -164,7 +164,7 @@ int spw_open (int mode)
int spw_update (const struct spwd *sp)
{
return commonio_update (&shadow_db, (const void *) sp);
return commonio_update (&shadow_db, sp);
}
int spw_remove (const char *name)

View File

@@ -355,13 +355,14 @@ static int subordinate_range_cmp (const void *p1, const void *p2)
{
struct subordinate_range *range1, *range2;
if ((*(struct commonio_entry **) p1)->eptr == NULL)
return 1;
if ((*(struct commonio_entry **) p2)->eptr == NULL)
return -1;
range1 = ((struct subordinate_range *) (*(struct commonio_entry **) p1)->eptr);
range2 = ((struct subordinate_range *) (*(struct commonio_entry **) p2)->eptr);
range1 = (*(struct commonio_entry **) p1)->eptr;
if (range1 == NULL)
return 1;
range2 = (*(struct commonio_entry **) p2)->eptr;
if (range2 == NULL)
return -1;
if (range1->start < range2->start)
return -1;

View File

@@ -141,7 +141,7 @@ static /*@null@*/ char *shadowtcb_path_rel_existing (const char *name)
shadow_progname, link);
return NULL;
}
link[(size_t)ret] = '\0';
link[ret] = '\0';
rval = strdup (link);
if (NULL == rval) {
OUT_OF_MEMORY;