Message ID | 20180922000859.121485-1-nnk@google.com (mailing list archive) |
---|---|
State | Not Applicable |
Headers | show |
Series | secilc: better error handling | expand |
On Fri, Sep 21, 2018 at 5:12 PM Nick Kralevich via Selinux < selinux@tycho.nsa.gov> wrote: > Fix a situation where the secilc command line tool could return success > even though the compilation failed. > > $ secilc /dev/null -o /dev/null -f /dev/null > Failure reading file: /dev/null > $ echo $? > 0 > > Fix a few other minor oversights while I'm here. > I'd prefer this split into at least 2 patches on the off chance we need to revert the actual code changes we don't lose the spelling and whitespace fixes. Otherwise LGTM. > > Signed-off-by: Nick Kralevich <nnk@google.com> > --- > libsepol/include/sepol/errcodes.h | 2 +- > secilc/secilc.c | 8 ++++++-- > 2 files changed, 7 insertions(+), 3 deletions(-) > > diff --git a/libsepol/include/sepol/errcodes.h > b/libsepol/include/sepol/errcodes.h > index 0136564a..6e9ff316 100644 > --- a/libsepol/include/sepol/errcodes.h > +++ b/libsepol/include/sepol/errcodes.h > @@ -12,7 +12,7 @@ extern "C" { > #define SEPOL_OK 0 > > /* These first error codes are defined for compatibility with > - * previous version of libsepol. In the future, custome error > + * previous version of libsepol. In the future, custom error > * codes that don't map to system error codes should be defined > * outside of the range of system error codes. > */ > diff --git a/secilc/secilc.c b/secilc/secilc.c > index 0be6975b..8578cc26 100644 > --- a/secilc/secilc.c > +++ b/secilc/secilc.c > @@ -257,14 +257,16 @@ int main(int argc, char *argv[]) > rc = stat(argv[i], &filedata); > if (rc == -1) { > fprintf(stderr, "Could not stat file: %s\n", > argv[i]); > + rc = SEPOL_ERR; > goto exit; > } > - file_size = filedata.st_size; > + file_size = filedata.st_size; > > buffer = malloc(file_size); > rc = fread(buffer, file_size, 1, file); > if (rc != 1) { > fprintf(stderr, "Failure reading file: %s\n", > argv[i]); > + rc = SEPOL_ERR; > goto exit; > } > fclose(file); > @@ -345,11 +347,13 @@ int main(int argc, char *argv[]) > > if (file_contexts == NULL) { > fprintf(stderr, "Failed to open file_contexts file\n"); > + rc = SEPOL_ERR; > goto exit; > } > - > + > if (fwrite(fc_buf, sizeof(char), fc_size, file_contexts) != > fc_size) { > fprintf(stderr, "Failed to write file_contexts file\n"); > + rc = SEPOL_ERR; > goto exit; > } > > -- > 2.19.0.444.g18242da7ef-goog > > _______________________________________________ > Selinux mailing list > Selinux@tycho.nsa.gov > To unsubscribe, send email to Selinux-leave@tycho.nsa.gov. > To get help, send an email containing "help" to > Selinux-request@tycho.nsa.gov. > <div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 21, 2018 at 5:12 PM Nick Kralevich via Selinux <<a href="mailto:selinux@tycho.nsa.gov">selinux@tycho.nsa.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Fix a situation where the secilc command line tool could return success<br> even though the compilation failed.<br> <br> $ secilc /dev/null -o /dev/null -f /dev/null<br> Failure reading file: /dev/null<br> $ echo $?<br> 0<br> <br> Fix a few other minor oversights while I'm here.<br></blockquote><div><br></div><div>I'd prefer this split into at least 2 patches on the off chance we</div><div>need to revert the actual code changes we don't lose the spelling</div><div>and whitespace fixes. Otherwise LGTM.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <br> Signed-off-by: Nick Kralevich <<a href="mailto:nnk@google.com" target="_blank">nnk@google.com</a>><br> ---<br> libsepol/include/sepol/errcodes.h | 2 +-<br> secilc/secilc.c | 8 ++++++--<br> 2 files changed, 7 insertions(+), 3 deletions(-)<br> <br> diff --git a/libsepol/include/sepol/errcodes.h b/libsepol/include/sepol/errcodes.h<br> index 0136564a..6e9ff316 100644<br> --- a/libsepol/include/sepol/errcodes.h<br> +++ b/libsepol/include/sepol/errcodes.h<br> @@ -12,7 +12,7 @@ extern "C" {<br> #define SEPOL_OK 0<br> <br> /* These first error codes are defined for compatibility with<br> - * previous version of libsepol. In the future, custome error<br> + * previous version of libsepol. In the future, custom error<br> * codes that don't map to system error codes should be defined<br> * outside of the range of system error codes.<br> */<br> diff --git a/secilc/secilc.c b/secilc/secilc.c<br> index 0be6975b..8578cc26 100644<br> --- a/secilc/secilc.c<br> +++ b/secilc/secilc.c<br> @@ -257,14 +257,16 @@ int main(int argc, char *argv[])<br> rc = stat(argv[i], &filedata);<br> if (rc == -1) {<br> fprintf(stderr, "Could not stat file: %s\n", argv[i]);<br> + rc = SEPOL_ERR;<br> goto exit;<br> }<br> - file_size = filedata.st_size; <br> + file_size = filedata.st_size;<br> <br> buffer = malloc(file_size);<br> rc = fread(buffer, file_size, 1, file);<br> if (rc != 1) {<br> fprintf(stderr, "Failure reading file: %s\n", argv[i]);<br> + rc = SEPOL_ERR;<br> goto exit;<br> }<br> fclose(file);<br> @@ -345,11 +347,13 @@ int main(int argc, char *argv[])<br> <br> if (file_contexts == NULL) {<br> fprintf(stderr, "Failed to open file_contexts file\n");<br> + rc = SEPOL_ERR;<br> goto exit;<br> }<br> - <br> +<br> if (fwrite(fc_buf, sizeof(char), fc_size, file_contexts) != fc_size) {<br> fprintf(stderr, "Failed to write file_contexts file\n");<br> + rc = SEPOL_ERR;<br> goto exit;<br> }<br> <br> -- <br> 2.19.0.444.g18242da7ef-goog<br> <br> _______________________________________________<br> Selinux mailing list<br> <a href="mailto:Selinux@tycho.nsa.gov" target="_blank">Selinux@tycho.nsa.gov</a><br> To unsubscribe, send email to <a href="mailto:Selinux-leave@tycho.nsa.gov" target="_blank">Selinux-leave@tycho.nsa.gov</a>.<br> To get help, send an email containing "help" to <a href="mailto:Selinux-request@tycho.nsa.gov" target="_blank">Selinux-request@tycho.nsa.gov</a>.<br> </blockquote></div></div>
diff --git a/libsepol/include/sepol/errcodes.h b/libsepol/include/sepol/errcodes.h index 0136564a..6e9ff316 100644 --- a/libsepol/include/sepol/errcodes.h +++ b/libsepol/include/sepol/errcodes.h @@ -12,7 +12,7 @@ extern "C" { #define SEPOL_OK 0 /* These first error codes are defined for compatibility with - * previous version of libsepol. In the future, custome error + * previous version of libsepol. In the future, custom error * codes that don't map to system error codes should be defined * outside of the range of system error codes. */ diff --git a/secilc/secilc.c b/secilc/secilc.c index 0be6975b..8578cc26 100644 --- a/secilc/secilc.c +++ b/secilc/secilc.c @@ -257,14 +257,16 @@ int main(int argc, char *argv[]) rc = stat(argv[i], &filedata); if (rc == -1) { fprintf(stderr, "Could not stat file: %s\n", argv[i]); + rc = SEPOL_ERR; goto exit; } - file_size = filedata.st_size; + file_size = filedata.st_size; buffer = malloc(file_size); rc = fread(buffer, file_size, 1, file); if (rc != 1) { fprintf(stderr, "Failure reading file: %s\n", argv[i]); + rc = SEPOL_ERR; goto exit; } fclose(file); @@ -345,11 +347,13 @@ int main(int argc, char *argv[]) if (file_contexts == NULL) { fprintf(stderr, "Failed to open file_contexts file\n"); + rc = SEPOL_ERR; goto exit; } - + if (fwrite(fc_buf, sizeof(char), fc_size, file_contexts) != fc_size) { fprintf(stderr, "Failed to write file_contexts file\n"); + rc = SEPOL_ERR; goto exit; }
Fix a situation where the secilc command line tool could return success even though the compilation failed. $ secilc /dev/null -o /dev/null -f /dev/null Failure reading file: /dev/null $ echo $? 0 Fix a few other minor oversights while I'm here. Signed-off-by: Nick Kralevich <nnk@google.com> --- libsepol/include/sepol/errcodes.h | 2 +- secilc/secilc.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-)