From dc54db906945ac1db84582caeb4c47d4cb89acb5 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Mon, 10 Oct 2016 07:43:57 +0200 Subject: Make error_set() return NULL for convenience This often eliminates the need for a block where one line is enough. --- liberty-proto.c | 25 +++++++------------------ liberty.c | 31 ++++++++++++------------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/liberty-proto.c b/liberty-proto.c index 9ecd656..602cde6 100644 --- a/liberty-proto.c +++ b/liberty-proto.c @@ -647,10 +647,7 @@ scgi_parser_push (struct scgi_parser *self, if (!len) { if (self->state != SCGI_READING_CONTENT) - { - error_set (e, "premature EOF"); - return false; - } + return error_set (e, "premature EOF"); // Indicate end of file return self->on_content (self->user_data, NULL, 0); @@ -678,17 +675,12 @@ scgi_parser_push (struct scgi_parser *self, } if (digit < '0' || digit >= '9') - { - error_set (e, "invalid header netstring"); - return false; - } + return error_set (e, "invalid header netstring"); size_t new_len = self->headers_len * 10 + (digit - '0'); if (new_len < self->headers_len) - { - error_set (e, "header netstring is too long"); - return false; - } + return error_set (e, "header netstring is too long"); + self->headers_len = new_len; str_remove_slice (&self->input, 0, 1); break; @@ -704,10 +696,8 @@ scgi_parser_push (struct scgi_parser *self, // The netstring is ending but we haven't finished parsing it, // or the netstring doesn't end with a comma if (self->name.len || c != ',') - { - error_set (e, "invalid header netstring"); - return false; - } + return error_set (e, "invalid header netstring"); + self->state = SCGI_READING_CONTENT; keep_running = self->on_headers_read (self->user_data); } @@ -728,8 +718,7 @@ scgi_parser_push (struct scgi_parser *self, if (!self->headers_len) { // The netstring is ending but we haven't finished parsing it - error_set (e, "invalid header netstring"); - return false; + return error_set (e, "invalid header netstring"); } else if (c != '\0') str_append_c (&self->value, c); diff --git a/liberty.c b/liberty.c index 997fae7..ce57f63 100644 --- a/liberty.c +++ b/liberty.c @@ -633,14 +633,14 @@ struct error char *message; ///< Textual description of the event }; -static void +static bool error_set (struct error **e, const char *message, ...) ATTRIBUTE_PRINTF (2, 3); -static void +static bool error_set (struct error **e, const char *message, ...) { if (!e) - return; + return false; va_list ap; va_start (ap, message); @@ -660,6 +660,7 @@ error_set (struct error **e, const char *message, ...) soft_assert (*e == NULL); *e = tmp; + return false; } static void @@ -725,10 +726,7 @@ random_bytes (void *output, size_t len, struct error **e) ssize_t got = 0; if (fd < 0) - { - error_set (e, "%s: %s", "open", strerror (errno)); - return false; - } + return error_set (e, "%s: %s", "open", strerror (errno)); else if ((got = read (fd, output, len)) < 0) error_set (e, "%s: %s", "read", strerror (errno)); else if (got != (ssize_t) len) @@ -3113,16 +3111,14 @@ ensure_directory_existence (const char *path, struct error **e) { if (mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO)) { - error_set (e, "cannot create directory `%s': %s", + return error_set (e, "cannot create directory `%s': %s", path, strerror (errno)); - return false; } } else if (!S_ISDIR (st.st_mode)) { - error_set (e, "cannot create directory `%s': %s", + return error_set (e, "cannot create directory `%s': %s", path, "file exists but is not a directory"); - return false; } return true; } @@ -3425,9 +3421,8 @@ read_file (const char *filename, struct str *output, struct error **e) FILE *fp = fopen (filename, "rb"); if (!fp) { - error_set (e, "could not open `%s' for reading: %s", + return error_set (e, "could not open `%s' for reading: %s", filename, strerror (errno)); - return false; } char buf[BUFSIZ]; @@ -3443,9 +3438,8 @@ read_file (const char *filename, struct str *output, struct error **e) if (success) return true; - error_set (e, "error while reading `%s': %s", + return error_set (e, "error while reading `%s': %s", filename, strerror (errno)); - return false; } /// Overwrites filename contents with data; creates directories as needed @@ -3462,9 +3456,8 @@ write_file (const char *filename, const void *data, size_t data_len, FILE *fp = fopen (filename, "w"); if (!fp) { - error_set (e, "could not open `%s' for writing: %s", + return error_set (e, "could not open `%s' for writing: %s", filename, strerror (errno)); - return false; } fwrite (data, data_len, 1, fp); @@ -3474,8 +3467,8 @@ write_file (const char *filename, const void *data, size_t data_len, if (!success) { - error_set (e, "writing to `%s' failed: %s", filename, strerror (errno)); - return false; + return error_set (e, "writing to `%s' failed: %s", + filename, strerror (errno)); } return true; } -- cgit v1.2.3-70-g09d2