From 9e31065b5e1c1437bf1481175b0d10cc3d18a779 Mon Sep 17 00:00:00 2001 From: nekral-guest Date: Mon, 26 May 2008 09:39:29 +0000 Subject: [PATCH] * lib/sgetspent.c: Add brackets and parenthesis. * lib/sgetspent.c: Avoid assignments in comparisons. * lib/sgetspent.c: Avoid implicit conversion of pointers / chars to booleans. * lib/sgetspent.c: Avoid multi-statements lines. --- ChangeLog | 8 +++++ lib/sgetspent.c | 81 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index a46972eb..a9136cd0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2008-05-26 Nicolas François + + * lib/sgetspent.c: Add brackets and parenthesis. + * lib/sgetspent.c: Avoid assignments in comparisons. + * lib/sgetspent.c: Avoid implicit conversion of pointers / chars + to booleans. + * lib/sgetspent.c: Avoid multi-statements lines. + 2008-05-26 Nicolas François * lib/sgetpwent.c: Avoid implicit conversion of pointers / chars to diff --git a/lib/sgetspent.c b/lib/sgetspent.c index 6b396b69..be55e77d 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -56,32 +56,41 @@ struct spwd *sgetspent (const char *string) * have to do that to our private copy. */ - if (strlen (string) >= sizeof spwbuf) + if (strlen (string) >= sizeof spwbuf) { return 0; /* fail if too long */ + } strcpy (spwbuf, string); - if ((cp = strrchr (spwbuf, '\n'))) + cp = strrchr (spwbuf, '\n'); + if (NULL != cp) { *cp = '\0'; + } /* * Tokenize the string into colon separated fields. Allow up to * FIELDS different fields. */ - for (cp = spwbuf, i = 0; *cp && i < FIELDS; i++) { + for (cp = spwbuf, i = 0; ('\0' != *cp) && (i < FIELDS); i++) { fields[i] = cp; - while (*cp && *cp != ':') + while (('\0' != *cp) && (':' != *cp)) { cp++; + } - if (*cp) - *cp++ = '\0'; + if ('\0' != *cp) { + *cp = '\0'; + cp++; + } } - if (i == (FIELDS - 1)) + if (i == (FIELDS - 1)) { fields[i++] = cp; + } - if ((cp && *cp) || (i != FIELDS && i != OFIELDS)) + if ( ((NULL != cp) && ('\0' != *cp)) || + ((i != FIELDS) && (i != OFIELDS)) ) { return 0; + } /* * Start populating the structure. The fields are all in @@ -97,28 +106,37 @@ struct spwd *sgetspent (const char *string) * incorrectly formatted number. */ - if ((spwd.sp_lstchg = strtol (fields[2], &cpp, 10)) == 0 && *cpp) { + spwd.sp_lstchg = strtol (fields[2], &cpp, 10); + /* FIXME: (0 == spwd.sp_lstchg) does not look correct */ + if ((0 == spwd.sp_lstchg) && ('\0' != *cpp)) { return 0; - } else if (fields[2][0] == '\0') + } else if (fields[2][0] == '\0') { spwd.sp_lstchg = -1; + } /* * Get the minimum period between password changes. */ - if ((spwd.sp_min = strtol (fields[3], &cpp, 10)) == 0 && *cpp) { + spwd.sp_min = strtol (fields[3], &cpp, 10); + /* FIXME: (0 == spwd.sp_min) does not look correct */ + if ((0 == spwd.sp_min) && ('\0' != *cpp)) { return 0; - } else if (fields[3][0] == '\0') + } else if (fields[3][0] == '\0') { spwd.sp_min = -1; + } /* * Get the maximum number of days a password is valid. */ - if ((spwd.sp_max = strtol (fields[4], &cpp, 10)) == 0 && *cpp) { + spwd.sp_max = strtol (fields[4], &cpp, 10); + /* FIXME: (0 == spwd.sp_max) does not look correct */ + if ((0 == spwd.sp_max) && ('\0' != *cpp)) { return 0; - } else if (fields[4][0] == '\0') + } else if (fields[4][0] == '\0') { spwd.sp_max = -1; + } /* * If there are only OFIELDS fields (this is a SVR3.2 /etc/shadow @@ -126,8 +144,10 @@ struct spwd *sgetspent (const char *string) */ if (i == OFIELDS) { - spwd.sp_warn = spwd.sp_inact = spwd.sp_expire = - spwd.sp_flag = -1; + spwd.sp_warn = -1; + spwd.sp_inact = -1; + spwd.sp_expire = -1; + spwd.sp_flag = -1; return &spwd; } @@ -136,40 +156,53 @@ struct spwd *sgetspent (const char *string) * Get the number of days of password expiry warning. */ - if ((spwd.sp_warn = strtol (fields[5], &cpp, 10)) == 0 && *cpp) { + spwd.sp_warn = strtol (fields[5], &cpp, 10); + /* FIXME: (0 == spwd.sp_warn) does not look correct */ + if ((0 == spwd.sp_warn) && ('\0' != *cpp)) { return 0; - } else if (fields[5][0] == '\0') + } else if (fields[5][0] == '\0') { spwd.sp_warn = -1; + } /* * Get the number of days of inactivity before an account is * disabled. */ - if ((spwd.sp_inact = strtol (fields[6], &cpp, 10)) == 0 && *cpp) { + spwd.sp_inact = strtol (fields[6], &cpp, 10); + /* FIXME: (0 == spwd.sp_inact) does not look correct */ + if ((0 == spwd.sp_inact) && ('\0' != *cpp)) { return 0; - } else if (fields[6][0] == '\0') + } else if (fields[6][0] == '\0') { spwd.sp_inact = -1; + } /* * Get the number of days after the epoch before the account is * set to expire. */ - if ((spwd.sp_expire = strtol (fields[7], &cpp, 10)) == 0 && *cpp) { + spwd.sp_expire = strtol (fields[7], &cpp, 10); + /* FIXME: (0 == spwd.sp_expire) does not look correct */ + if ((0 == spwd.sp_expire) && ('\0' != *cpp)) { return 0; - } else if (fields[7][0] == '\0') + } else if (fields[7][0] == '\0') { spwd.sp_expire = -1; + } /* * This field is reserved for future use. But it isn't supposed * to have anything other than a valid integer in it. */ - if ((spwd.sp_flag = strtol (fields[8], &cpp, 10)) == 0 && *cpp) { + spwd.sp_flag = strtol (fields[8], &cpp, 10); + /* FIXME: (0 == spwd.sp_flag) does not look correct */ + if ((0 == spwd.sp_flag) && ('\0' != *cpp)) { return 0; - } else if (fields[8][0] == '\0') + } else if (fields[8][0] == '\0') { spwd.sp_flag = -1; + } return (&spwd); } +