From: Stephen Smalley <sds@tycho.nsa.gov>
To: James Carter <jwcart2@tycho.nsa.gov>, selinux@tycho.nsa.gov
Subject: Re: [PATCH v4] Replace fmemopen() with internal function in libsepol.
Date: Fri, 08 May 2015 10:57:07 -0400 [thread overview]
Message-ID: <554CCEC3.6070102@tycho.nsa.gov> (raw)
In-Reply-To: <1431096669-14399-1-git-send-email-jwcart2@tycho.nsa.gov>
On 05/08/2015 10:51 AM, James Carter wrote:
> Created a new function, get_line(), to replace the use of fmemopen()
> and getline() in module_to_cil.c since fmemopen() is not available
> on Darwin.
>
> Signed-off-by: James Carter <jwcart2@tycho.nsa.gov>
Acked-by: Stephen Smalley <sds@tycho.nsa.gov>
> ---
> libsepol/src/module_to_cil.c | 122 +++++++++++++++++++++++--------------------
> 1 file changed, 65 insertions(+), 57 deletions(-)
>
> diff --git a/libsepol/src/module_to_cil.c b/libsepol/src/module_to_cil.c
> index d1d2efe..8d16d3e 100644
> --- a/libsepol/src/module_to_cil.c
> +++ b/libsepol/src/module_to_cil.c
> @@ -107,6 +107,44 @@ static void cil_println(int indent, const char *fmt, ...)
> }
> }
>
> +static int get_line(char **start, char *end, char **line)
> +{
> + int rc = 1;
> + char *p = NULL;
> + size_t len = 0;
> +
> + *line = NULL;
> +
> + for (p = *start; p < end && isspace(*p); p++);
> +
> + *start = p;
> +
> + for (len = 0; p < end && *p != '\n' && *p != '\0'; p++, len++);
> +
> + if (len == 0) {
> + rc = 0;
> + goto exit;
> + }
> +
> + *line = malloc(len+1);
> + if (*line == NULL) {
> + log_err("Out of memory");
> + rc = -1;
> + goto exit;
> + }
> +
> + memcpy(*line, *start, len);
> + (*line)[len] = '\0';
> +
> + *start = p;
> +
> + return rc;
> +
> +exit:
> + *start = NULL;
> + return rc;
> +}
> +
> struct map_args {
> struct policydb *pdb;
> struct avrule_block *block;
> @@ -2907,14 +2945,11 @@ exit:
> static int seusers_to_cil(struct sepol_module_package *mod_pkg)
> {
> int rc = -1;
> - FILE *fp = NULL;
> char *seusers = sepol_module_package_get_seusers(mod_pkg);
> size_t seusers_len = sepol_module_package_get_seusers_len(mod_pkg);
> - size_t len = 0;
> + char *cur = seusers;
> + char *end = seusers + seusers_len;
> char *line = NULL;
> - ssize_t line_len = 0;
> - char *buf = NULL;
> -
> char *user = NULL;
> char *seuser = NULL;
> char *level = NULL;
> @@ -2924,19 +2959,12 @@ static int seusers_to_cil(struct sepol_module_package *mod_pkg)
> return 0;
> }
>
> - fp = fmemopen(seusers, seusers_len, "r");
> -
> - while ((line_len = getline(&line, &len, fp)) != -1) {
> - buf = line;
> - buf[line_len - 1] = '\0';
> - while (*buf && isspace(buf[0])) {
> - buf++;
> - }
> - if (buf[0] == '#' || buf[0] == '\0') {
> + while ((rc = get_line(&cur, end, &line)) > 0) {
> + if (line[0] == '#') {
> continue;
> }
>
> - matched = sscanf(buf, "%m[^:]:%m[^:]:%ms", &user, &seuser, &level);
> + matched = sscanf(line, "%m[^:]:%m[^:]:%ms", &user, &seuser, &level);
>
> if (matched < 2 || matched > 3) {
> log_err("Invalid seuser line: %s", line);
> @@ -2964,20 +2992,17 @@ static int seusers_to_cil(struct sepol_module_package *mod_pkg)
> free(user);
> free(seuser);
> free(level);
> + free(line);
> user = seuser = level = NULL;
> }
> - if (ferror(fp)) {
> +
> + if (rc == -1) {
> cil_printf("Failed to read seusers\n");
> - rc = -1;
> goto exit;
> }
>
> rc = 0;
> -
> exit:
> - if (fp != NULL) {
> - fclose(fp);
> - }
> free(line);
> free(user);
> free(seuser);
> @@ -3002,10 +3027,9 @@ static int user_extra_to_cil(struct sepol_module_package *mod_pkg)
> int rc = -1;
> char *userx = sepol_module_package_get_user_extra(mod_pkg);
> size_t userx_len = sepol_module_package_get_user_extra_len(mod_pkg);
> - FILE *fp = NULL;
> - size_t len = 0;
> - char *line = NULL;
> - ssize_t line_len = 0;
> + char *cur = userx;
> + char *end = userx + userx_len;
> + char *line;
> int matched;
> char *user = NULL;
> char *prefix = NULL;
> @@ -3014,10 +3038,10 @@ static int user_extra_to_cil(struct sepol_module_package *mod_pkg)
> return 0;
> }
>
> - fp = fmemopen(userx, userx_len, "r");
> -
> - while ((line_len = getline(&line, &len, fp)) != -1) {
> - line[line_len - 1] = '\0';
> + while ((rc = get_line(&cur, end, &line)) > 0) {
> + if (line[0] == '#') {
> + continue;
> + }
>
> matched = sscanf(line, "user %ms prefix %m[^;];", &user, &prefix);
> if (matched != 2) {
> @@ -3029,20 +3053,17 @@ static int user_extra_to_cil(struct sepol_module_package *mod_pkg)
> cil_println(0, "(userprefix %s %s)", user, prefix);
> free(user);
> free(prefix);
> - user = prefix = NULL;
> + free(line);
> + user = prefix = line = NULL;
> }
>
> - if (ferror(fp)) {
> + if (rc == -1) {
> cil_printf("Failed to read user_extra\n");
> - rc = -1;
> goto exit;
> }
>
> rc = 0;
> exit:
> - if (fp != NULL) {
> - fclose(fp);
> - }
> free(line);
> free(user);
> free(prefix);
> @@ -3055,11 +3076,9 @@ static int file_contexts_to_cil(struct sepol_module_package *mod_pkg)
> int rc = -1;
> char *fc = sepol_module_package_get_file_contexts(mod_pkg);
> size_t fc_len = sepol_module_package_get_file_contexts_len(mod_pkg);
> - FILE *fp = NULL;
> - size_t len = 0;
> + char *cur = fc;
> + char *end = fc + fc_len;
> char *line = NULL;
> - char *buf = NULL;
> - ssize_t line_len = 0;
> int matched;
> char *regex = NULL;
> char *mode = NULL;
> @@ -3070,20 +3089,12 @@ static int file_contexts_to_cil(struct sepol_module_package *mod_pkg)
> return 0;
> }
>
> - fp = fmemopen(fc, fc_len, "r");
> - while ((line_len = getline(&line, &len, fp)) != -1) {
> - buf = line;
> - if (buf[line_len - 1] == '\n') {
> - buf[line_len - 1] = '\0';
> - }
> - while (*buf && isspace(buf[0])) {
> - buf++;
> - }
> - if (buf[0] == '#' || buf[0] == '\0') {
> + while ((rc = get_line(&cur, end, &line)) > 0) {
> + if (line[0] == '#') {
> continue;
> }
>
> - matched = sscanf(buf, "%ms %ms %ms", ®ex, &mode, &context);
> + matched = sscanf(line, "%ms %ms %ms", ®ex, &mode, &context);
> if (matched < 2 || matched > 3) {
> rc = -1;
> log_err("Invalid file context line: %s", line);
> @@ -3130,12 +3141,12 @@ static int file_contexts_to_cil(struct sepol_module_package *mod_pkg)
> free(regex);
> free(mode);
> free(context);
> - regex = mode = context = NULL;
> + free(line);
> + regex = mode = context = line = NULL;
> }
>
> - if (ferror(fp)) {
> - cil_printf("Failed to read user_extra\n");
> - rc = -1;
> + if (rc == -1) {
> + cil_printf("Failed to read file_contexts_to_cil\n");
> goto exit;
> }
>
> @@ -3145,9 +3156,6 @@ exit:
> free(regex);
> free(mode);
> free(context);
> - if (fp != NULL) {
> - fclose(fp);
> - }
>
> return rc;
> }
>
next prev parent reply other threads:[~2015-05-08 14:57 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-08 14:51 [PATCH v4] Replace fmemopen() with internal function in libsepol James Carter
2015-05-08 14:57 ` Stephen Smalley [this message]
2015-05-08 15:05 ` James Carter
2015-05-08 21:34 ` Jeffrey Vander Stoep
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=554CCEC3.6070102@tycho.nsa.gov \
--to=sds@tycho.nsa.gov \
--cc=jwcart2@tycho.nsa.gov \
--cc=selinux@tycho.nsa.gov \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.