Skip to content

Commit

Permalink
contrib/, lib/, src/: Consistently use sizeof() as if it were a function
Browse files Browse the repository at this point in the history
sizeof(foo)

-  No spaces.
	Not:  sizeof (foo)
-  Parentheses.
	Not:  sizeof foo
-  No parentheses wrapping sizeof itself
	Not:  (sizeof foo)

This patch can be approximated with the following semantic patch:

	$ cat ~/tmp/spatch/sizeof.sp
	@@
	identifier a, b;
	@@

	- sizeof a->b
	+ sizeof(a->b)

	@@
	identifier a, b;
	@@

	- sizeof a.b
	+ sizeof(a.b)

	@@
	identifier x;
	@@

	- sizeof x
	+ sizeof(x)

	@@
	identifier x;
	@@

	- sizeof *x
	+ sizeof(*x)

	@@
	identifier x;
	@@

	- (sizeof(x))
	+ sizeof(x)

	@@
	identifier x;
	@@

	- (sizeof(*x))
	+ sizeof(*x)

Applied as

	$ find contrib/ lib* src/ -type f \
	| xargs spatch --sp-file ~/tmp/spatch/sizeof.sp --in-place;

The differences between the actual patch and the approximation via the
semantic patch from above are whitespace only.  When reviewing, it'll
be useful to diff with '-w'.

Link: <https://lkml.org/lkml/2012/7/11/103>
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Dec 10, 2024
1 parent 5c3f570 commit f8d0c00
Show file tree
Hide file tree
Showing 39 changed files with 128 additions and 128 deletions.
32 changes: 16 additions & 16 deletions contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ main (void)
printf ("\nLogin to add (^C to quit): ");
fflush (stdout);

safeget (usrname, sizeof (usrname));
safeget(usrname, sizeof(usrname));

if (!strlen (usrname))
{
Expand Down Expand Up @@ -239,10 +239,10 @@ main (void)

printf ("\nFull Name [%s]: ", usrname);
fflush (stdout);
safeget (person, sizeof (person));
safeget(person, sizeof(person));
if (!strlen (person))
{
bzero (person, sizeof (person));
bzero(person, sizeof(person));
strcpy (person, usrname);
};

Expand All @@ -253,7 +253,7 @@ main (void)
bad = 0;
printf ("GID [%d]: ", DEFAULT_GROUP);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
if (!strlen (foo))
group = DEFAULT_GROUP;
else if (isdigit (*foo))
Expand Down Expand Up @@ -294,7 +294,7 @@ main (void)
printf ("\nIf home dir ends with a / then '%s' will be appended to it\n", usrname);
printf ("Home Directory [%s/%s]: ", DEFAULT_HOME, usrname);
fflush (stdout);
safeget (dir, sizeof (dir));
safeget(dir, sizeof(dir));
if (!strlen(dir)) /* hit return */
sprintf(dir, "%s/%s", DEFAULT_HOME, usrname);
else if (dir[strlen (dir) - 1] == '/')
Expand All @@ -308,7 +308,7 @@ main (void)

printf ("\nShell [%s]: ", DEFAULT_SHELL);
fflush (stdout);
safeget (shell, sizeof (shell));
safeget(shell, sizeof(shell));
if (!strlen (shell))
strcpy(shell, DEFAULT_SHELL);
else
Expand Down Expand Up @@ -340,31 +340,31 @@ main (void)
#endif
printf ("\nMin. Password Change Days [%d]: ", DEFAULT_MIN_PASS);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
if (strlen (foo) > 1)
min_pass = DEFAULT_MIN_PASS;
else
min_pass = atoi (foo);

printf ("Max. Password Change Days [%d]: ", DEFAULT_MAX_PASS);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
if (strlen (foo) > 1)
max_pass = atoi (foo);
else
max_pass = DEFAULT_MAX_PASS;

printf ("Password Warning Days [%d]: ", DEFAULT_WARN_PASS);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
warn_pass = atoi (foo);
if (warn_pass == 0)

warn_pass = DEFAULT_WARN_PASS;

printf ("Days after Password Expiry for Account Locking [%d]: ", DEFAULT_USER_DIE);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
user_die = atoi (foo);
if (user_die == 0)
user_die = DEFAULT_USER_DIE;
Expand All @@ -388,7 +388,7 @@ main (void)
min_pass, max_pass, warn_pass, user_die);
printf ("\nIs this correct? [y/N]: ");
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));

done = bad = correct = (strprefix(foo, "y") || strprefix(foo, "Y"));

Expand All @@ -401,7 +401,7 @@ main (void)

*environ = NULL;

bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
sprintf (cmd, "%s -g %d -d %s -s %s -c \"%s\" -m -k /etc/skel %s",
USERADD_PATH, group, dir, shell, person, usrname);
printf ("Calling useradd to add new user:\n%s\n", cmd);
Expand All @@ -419,7 +419,7 @@ main (void)
*/
setuid (0);

bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));

/* Chage runs suid root. => we need ruid root to run it with
* anything other than chage -l
Expand All @@ -439,7 +439,7 @@ main (void)
/* I want to add a user completely with one easy command --chris */

#ifdef HAVE_QUOTAS
bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
sprintf (cmd, "%s -p %s -u %s", EDQUOTA_PATH, QUOTA_DEFAULT, usrname);
printf ("%s\n", cmd);
if (system (cmd))
Expand All @@ -453,7 +453,7 @@ main (void)
printf ("\nDefault quota set.\n");
#endif /* HAVE_QUOTAS */

bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
sprintf (cmd, "%s %s", PASSWD_PATH, usrname);
if (system (cmd))
{
Expand All @@ -463,7 +463,7 @@ main (void)
#endif
}
#ifdef IMMEDIATE_CHANGE
bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
sprintf (cmd, "%s -e %s", PASSWD_PATH, usrname);
if (system (cmd))
{
Expand Down
2 changes: 1 addition & 1 deletion lib/addgrps.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ add_groups(const char *list)
int ret;
FILE *shadow_logfd = log_get_logfd();

if (strlen (list) >= sizeof (buf)) {
if (strlen(list) >= sizeof(buf)) {
errno = EINVAL;
return -1;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
errno = EINVAL;
return 0;
}
len = read (fd, buf, sizeof (buf) - 1);
len = read(fd, buf, sizeof(buf) - 1);
close (fd);
if (len <= 0) {
if (log) {
Expand Down Expand Up @@ -776,7 +776,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
entries[n] = ptr;
n++;
}
qsort (entries, n, sizeof (struct commonio_entry *), cmp);
qsort(entries, n, sizeof(struct commonio_entry *), cmp);

/* Take care of the head and tail separately */
db->head = entries[0];
Expand Down Expand Up @@ -914,7 +914,7 @@ int commonio_close (struct commonio_db *db)
goto fail;
}

memzero (&sb, sizeof sb);
memzero(&sb, sizeof(sb));
if (NULL != db->fp) {
if (fstat (fileno (db->fp), &sb) != 0) {
(void) fclose (db->fp);
Expand Down
2 changes: 1 addition & 1 deletion lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ is_listed(const char *cfgin, const char *tty, bool def)
* See if this tty is listed in the console file.
*/

while (fgets (buf, sizeof (buf), fp) != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
stpsep(buf, "\n");
if (streq(buf, tty)) {
(void) fclose (fp);
Expand Down
2 changes: 1 addition & 1 deletion lib/copydir.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ static int copy_file (const struct path_info *src, const struct path_info *dst,
char buf[8192];
ssize_t cnt;

cnt = read (ifd, buf, sizeof buf);
cnt = read(ifd, buf, sizeof(buf));
if (cnt < 0) {
if (errno == EINTR) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void set_env (int argc, char *const *argv)
char *cp;

for (; argc > 0; argc--, argv++) {
if (strlen (*argv) >= sizeof variable) {
if (strlen(*argv) >= sizeof(variable)) {
continue; /* ignore long entries */
}

Expand Down
14 changes: 7 additions & 7 deletions lib/failure.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
void failure (uid_t uid, const char *tty, struct faillog *fl)
{
int fd;
off_t offset_uid = (off_t) (sizeof *fl) * uid;
off_t offset_uid = (off_t) sizeof(*fl) * uid;

/*
* Don't do anything if failure logging isn't set up.
Expand All @@ -59,15 +59,15 @@ void failure (uid_t uid, const char *tty, struct faillog *fl)
*/

if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (read (fd, 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.
*
* If lseek() or read() failed for any other reason, this
* might reset the counter. But the new failure will be
* logged.
*/
memzero (fl, sizeof *fl);
memzero(fl, sizeof(*fl));
}

/*
Expand All @@ -92,7 +92,7 @@ void failure (uid_t uid, const char *tty, struct faillog *fl)
*/

if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (write_full(fd, fl, sizeof *fl) == -1)) {
|| (write_full(fd, fl, sizeof(*fl)) == -1)) {
goto err_write;
}

Expand Down Expand Up @@ -150,7 +150,7 @@ int failcheck (uid_t uid, struct faillog *fl, bool failed)
{
int fd;
struct faillog fail;
off_t offset_uid = (off_t) (sizeof *fl) * uid;
off_t offset_uid = (off_t) sizeof(*fl) * uid;

/*
* Suppress the check if the log file isn't there.
Expand Down Expand Up @@ -182,7 +182,7 @@ int failcheck (uid_t uid, struct faillog *fl, bool failed)
*/

if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (read (fd, fl, sizeof *fl) != (ssize_t) sizeof *fl)) {
|| (read(fd, fl, sizeof(*fl)) != (ssize_t) sizeof(*fl))) {
(void) close (fd);
return 1;
}
Expand All @@ -204,7 +204,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_full(fd, &fail, sizeof fail) == -1)) {
|| (write_full(fd, &fail, sizeof(fail)) == -1)) {
goto err_write;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ change_field(char *buf, size_t maxsize, const char *prompt)
char newf[200];
char *cp;

if (maxsize > sizeof (newf)) {
maxsize = sizeof (newf);
if (maxsize > sizeof(newf)) {
maxsize = sizeof(newf);
}

printf ("\t%s [%s]: ", prompt, buf);
Expand Down
2 changes: 1 addition & 1 deletion lib/getdate.y
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ yylex (void)
if (isalpha (c))
{
for (p = buff; (c = *yyInput++, isalpha (c)) || c == '.';)
if (p < &buff[sizeof buff - 1])
if (p < &buff[sizeof(buff) - 1])
*p++ = c;
stpcpy(p, "");
yyInput--;
Expand Down
4 changes: 2 additions & 2 deletions lib/getdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct itemdef {
{"MOTD_FIRSTONLY", NULL}, \


#define NUMDEFS (sizeof(def_table)/sizeof(def_table[0]))
#define NUMDEFS (sizeof(def_table) / sizeof(def_table[0]))
static struct itemdef def_table[] = {
{"CHFN_RESTRICT", NULL},
{"CONSOLE_GROUPS", NULL},
Expand Down Expand Up @@ -557,7 +557,7 @@ static void def_load (void)
/*
* Go through all of the lines in the file.
*/
while (fgets (buf, sizeof (buf), fp) != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL) {

/*
* Trim trailing whitespace.
Expand Down
2 changes: 1 addition & 1 deletion lib/hushed.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool hushed (const char *username)
if (NULL == fp) {
return false;
}
for (found = false; !found && (fgets (buf, sizeof buf, fp) == buf);) {
for (found = false; !found && (fgets(buf, sizeof(buf), fp) == buf);) {
stpsep(buf, "\n");
found = streq(buf, pw->pw_shell) ||
streq(buf, pw->pw_name);
Expand Down
8 changes: 4 additions & 4 deletions lib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void dolastlog (
* for this UID. Negative UID's will create problems, but ...
*/

offset = (off_t) pw->pw_uid * sizeof newlog;
offset = (off_t) pw->pw_uid * sizeof(newlog);

if (lseek (fd, offset, SEEK_SET) != offset) {
SYSLOG ((LOG_WARN,
Expand All @@ -71,8 +71,8 @@ void dolastlog (
* the way we read the old one in.
*/

if (read (fd, &newlog, sizeof newlog) != (ssize_t) sizeof newlog) {
memzero (&newlog, sizeof newlog);
if (read(fd, &newlog, sizeof(newlog)) != (ssize_t) sizeof(newlog)) {
memzero(&newlog, sizeof(newlog));
}
if (NULL != ll) {
*ll = newlog;
Expand All @@ -86,7 +86,7 @@ void dolastlog (
STRNCPY(newlog.ll_host, host);
#endif
if ( (lseek (fd, offset, SEEK_SET) != offset)
|| (write_full(fd, &newlog, sizeof newlog) == -1)) {
|| (write_full(fd, &newlog, sizeof(newlog)) == -1)) {
goto err_write;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/loginprompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ login_prompt(char *name, int namesize)
(void) fclose (fp);
}
}
(void) gethostname (buf, sizeof buf);
(void) gethostname(buf, sizeof(buf));
printf (_("\n%s login: "), buf);
(void) fflush (stdout);

Expand All @@ -83,7 +83,7 @@ login_prompt(char *name, int namesize)
*/

MEMZERO(buf);
if (fgets (buf, sizeof buf, stdin) != buf) {
if (fgets(buf, sizeof(buf), stdin) != buf) {
exit (EXIT_FAILURE);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/setupenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void read_env_file (const char *filename)
if (NULL == fp) {
return;
}
while (fgets (buf, (int)(sizeof buf), fp) == buf) {
while (fgets(buf, (int) sizeof(buf), fp) == buf) {
if (stpsep(buf, "\n") == NULL)
break;

Expand Down
2 changes: 1 addition & 1 deletion lib/sgetpwent.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sgetpwent(const char *buf)
* the password structure remain valid.
*/

if (strlen (buf) >= sizeof pwdbuf) {
if (strlen(buf) >= sizeof(pwdbuf)) {
fprintf (shadow_logfd,
"%s: Too long passwd entry encountered, file corruption?\n",
shadow_progname);
Expand Down
2 changes: 1 addition & 1 deletion lib/sgetspent.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sgetspent(const char *string)
* have to do that to our private copy.
*/

if (strlen (string) >= sizeof spwbuf) {
if (strlen(string) >= sizeof(spwbuf)) {
fprintf (shadow_logfd,
"%s: Too long passwd entry encountered, file corruption?\n",
shadow_progname);
Expand Down
2 changes: 1 addition & 1 deletion lib/shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct spwd *fgetspent (FILE * fp)
return (0);
}

if (fgets (buf, sizeof buf, fp) != NULL)
if (fgets(buf, sizeof(buf), fp) != NULL)
{
stpsep(buf, "\n");
return sgetspent(buf);
Expand Down
Loading

0 comments on commit f8d0c00

Please sign in to comment.