summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Safranek <jsafrane@redhat.com>2009-05-26 14:52:42 +0200
committerJan Safranek <jsafrane@redhat.com>2009-05-26 14:52:42 +0200
commit84da02ebfc7fbc2862ca238d00a53548b2e0b2b1 (patch)
tree6d69ea8e05a26aff1912c7a1e594fa9e314d6a8b
parent8315b5b9fb612d995d843361dbc58b3dd99bf1b2 (diff)
downloadlibcg-84da02ebfc7fbc2862ca238d00a53548b2e0b2b1.tar.gz
libcg-84da02ebfc7fbc2862ca238d00a53548b2e0b2b1.tar.xz
libcg-84da02ebfc7fbc2862ca238d00a53548b2e0b2b1.zip
Hi,
By using sizeof() in memset(), we will not need to care of the buffer size. Thanks Ken'ichi Ohmichi Signed-off-by: Ken'ichi Ohmichi <oomichi@mxs.nes.nec.co.jp>
-rw-r--r--src/api.c37
1 files changed, 13 insertions, 24 deletions
diff --git a/src/api.c b/src/api.c
index 4f88d20..75dc92a 100644
--- a/src/api.c
+++ b/src/api.c
@@ -268,7 +268,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
FILE *fp = NULL;
/* Buffer to store the line we're working on */
- char *buff = NULL;
+ char buff[CGROUP_RULE_MAXLINE] = { '\0' };
/* Iterator for the line we're working on */
char *itr = NULL;
@@ -322,14 +322,6 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
goto unlock;
}
- buff = calloc(CGROUP_RULE_MAXLINE, sizeof(char));
- if (!buff) {
- cgroup_dbg("Out of memory? Error: %s\n", strerror(errno));
- last_errno = errno;
- ret = ECGOTHER;
- goto close;
- }
-
/* Determine which list we're using. */
if (cache)
lst = &rl;
@@ -342,7 +334,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
/* Now, parse the configuration file one line at a time. */
cgroup_dbg("Parsing configuration file.\n");
- while ((itr = fgets(buff, CGROUP_RULE_MAXLINE, fp)) != NULL) {
+ while ((itr = fgets(buff, sizeof(buff), fp)) != NULL) {
linenum++;
/* We ignore anything after a # sign as comments. */
@@ -354,7 +346,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
*itr = '\0';
/* Now, skip any leading tabs and spaces. */
- itr = buff;
+ itr = &buff;
while (itr && isblank(*itr))
itr++;
@@ -369,7 +361,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
if (skipped && *itr == '%') {
cgroup_dbg("Warning: Skipped child of invalid rule,"
" line %d.\n", linenum);
- memset(buff, '\0', CGROUP_RULE_MAXLINE);
+ memset(buff, '\0', sizeof(buff));
continue;
}
@@ -378,9 +370,9 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
* there's an error in the configuration file.
*/
skipped = false;
- memset(user, '\0', LOGIN_NAME_MAX);
- memset(controllers, '\0', CG_CONTROLLER_MAX);
- memset(destination, '\0', FILENAME_MAX);
+ memset(user, '\0', sizeof(user));
+ memset(controllers, '\0', sizeof(controllers));
+ memset(destination, '\0', sizeof(destination));
i = sscanf(itr, "%s%s%s", user, controllers, destination);
if (i != 3) {
cgroup_dbg("Failed to parse configuration file on"
@@ -402,7 +394,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
cgroup_dbg("Parsing of configuration file"
" complete.\n\n");
ret = -1;
- goto cleanup;
+ goto close;
}
if (strncmp(user, "@", 1) == 0) {
/* New GID rule. */
@@ -414,7 +406,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
cgroup_dbg("Warning: Entry for %s not"
"found. Skipping rule on line"
" %d.\n", itr, linenum);
- memset(buff, '\0', CGROUP_RULE_MAXLINE);
+ memset(buff, '\0', sizeof(buff));
skipped = true;
continue;
}
@@ -431,7 +423,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
cgroup_dbg("Warning: Entry for %s not"
"found. Skipping rule on line"
" %d.\n", user, linenum);
- memset(buff, '\0', CGROUP_RULE_MAXLINE);
+ memset(buff, '\0', sizeof(buff));
skipped = true;
continue;
}
@@ -471,7 +463,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
strerror(errno));
last_errno = errno;
ret = ECGOTHER;
- goto cleanup;
+ goto close;
}
newrule->uid = uid;
@@ -524,7 +516,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
cgroup_dbg("\n");
/* Finally, clear the buffer. */
- memset(buff, '\0', CGROUP_RULE_MAXLINE);
+ memset(buff, '\0', sizeof(buff));
grp = NULL;
pwd = NULL;
}
@@ -532,7 +524,7 @@ static int cgroup_parse_rules(bool cache, uid_t muid, gid_t mgid)
/* If we make it here, there were no errors. */
cgroup_dbg("Parsing of configuration file complete.\n\n");
ret = (matched && !cache) ? -1 : 0;
- goto cleanup;
+ goto close;
destroyrule:
cgroup_free_rule(newrule);
@@ -540,9 +532,6 @@ destroyrule:
parsefail:
ret = ECGROUPPARSEFAIL;
-cleanup:
- free(buff);
-
close:
fclose(fp);
unlock: