Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <alx@kernel.org>
alejandro-colomar committed Aug 12, 2024
1 parent 8af482a commit 7cae428
Showing 40 changed files with 133 additions and 133 deletions.
32 changes: 16 additions & 16 deletions contrib/adduser.c
Original file line number Diff line number Diff line change
@@ -201,7 +201,7 @@ main (void)
printf ("\nLogin to add (^C to quit): ");
fflush (stdout);

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

if (!strlen (usrname))
{
@@ -236,10 +236,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);
};

@@ -250,7 +250,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))
@@ -291,7 +291,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] == '/')
@@ -305,7 +305,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
@@ -337,31 +337,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;
@@ -385,7 +385,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 = (foo[0] == 'y' || foo[0] == 'Y');

@@ -398,7 +398,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);
@@ -416,7 +416,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
@@ -436,7 +436,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))
@@ -450,7 +450,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))
{
@@ -460,7 +460,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))
{
2 changes: 1 addition & 1 deletion lib/addgrps.c
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions lib/commonio.c
Original file line number Diff line number Diff line change
@@ -186,7 +186,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) {
@@ -774,7 +774,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];
@@ -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);
2 changes: 1 addition & 1 deletion lib/console.c
Original file line number Diff line number Diff line change
@@ -74,7 +74,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 (strcmp (buf, tty) == 0) {
(void) fclose (fp);
2 changes: 1 addition & 1 deletion lib/copydir.c
Original file line number Diff line number Diff line change
@@ -796,7 +796,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;
2 changes: 1 addition & 1 deletion lib/env.c
Original file line number Diff line number Diff line change
@@ -162,7 +162,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 */
}

14 changes: 7 additions & 7 deletions lib/failure.c
Original file line number Diff line number Diff line change
@@ -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.
@@ -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));
}

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

@@ -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.
@@ -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;
}
@@ -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;
}

4 changes: 2 additions & 2 deletions lib/fields.c
Original file line number Diff line number Diff line change
@@ -73,8 +73,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);
2 changes: 1 addition & 1 deletion lib/getdate.y
Original file line number Diff line number Diff line change
@@ -770,7 +770,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--;
6 changes: 3 additions & 3 deletions lib/getdef.c
Original file line number Diff line number Diff line change
@@ -79,7 +79,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},
@@ -157,7 +157,7 @@ static struct itemdef def_table[] = {
{NULL, NULL}
};

#define NUMKNOWNDEFS (sizeof(knowndef_table)/sizeof(knowndef_table[0]))
#define NUMKNOWNDEFS (sizeof(knowndef_table) / sizeof(knowndef_table[0]))
static struct itemdef knowndef_table[] = {
#ifdef USE_PAM
PAMDEFS
@@ -556,7 +556,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.
2 changes: 1 addition & 1 deletion lib/hushed.c
Original file line number Diff line number Diff line change
@@ -72,7 +72,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 = (strcmp (buf, pw->pw_shell) == 0) ||
(strcmp (buf, pw->pw_name) == 0);
8 changes: 4 additions & 4 deletions lib/log.c
Original file line number Diff line number Diff line change
@@ -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,
@@ -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;
@@ -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;
}

4 changes: 2 additions & 2 deletions lib/loginprompt.c
Original file line number Diff line number Diff line change
@@ -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);

@@ -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);
}

2 changes: 1 addition & 1 deletion lib/setupenv.c
Original file line number Diff line number Diff line change
@@ -54,7 +54,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;

2 changes: 1 addition & 1 deletion lib/sgetpwent.c
Original file line number Diff line number Diff line change
@@ -50,7 +50,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);
2 changes: 1 addition & 1 deletion lib/sgetspent.c
Original file line number Diff line number Diff line change
@@ -48,7 +48,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);
Loading

0 comments on commit 7cae428

Please sign in to comment.