Make error_set() return NULL for convenience
This often eliminates the need for a block where one line is enough.
This commit is contained in:
parent
0b77bdeaf9
commit
dc54db9069
@ -647,10 +647,7 @@ scgi_parser_push (struct scgi_parser *self,
|
|||||||
if (!len)
|
if (!len)
|
||||||
{
|
{
|
||||||
if (self->state != SCGI_READING_CONTENT)
|
if (self->state != SCGI_READING_CONTENT)
|
||||||
{
|
return error_set (e, "premature EOF");
|
||||||
error_set (e, "premature EOF");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Indicate end of file
|
// Indicate end of file
|
||||||
return self->on_content (self->user_data, NULL, 0);
|
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')
|
if (digit < '0' || digit >= '9')
|
||||||
{
|
return error_set (e, "invalid header netstring");
|
||||||
error_set (e, "invalid header netstring");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t new_len = self->headers_len * 10 + (digit - '0');
|
size_t new_len = self->headers_len * 10 + (digit - '0');
|
||||||
if (new_len < self->headers_len)
|
if (new_len < self->headers_len)
|
||||||
{
|
return error_set (e, "header netstring is too long");
|
||||||
error_set (e, "header netstring is too long");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
self->headers_len = new_len;
|
self->headers_len = new_len;
|
||||||
str_remove_slice (&self->input, 0, 1);
|
str_remove_slice (&self->input, 0, 1);
|
||||||
break;
|
break;
|
||||||
@ -704,10 +696,8 @@ scgi_parser_push (struct scgi_parser *self,
|
|||||||
// The netstring is ending but we haven't finished parsing it,
|
// The netstring is ending but we haven't finished parsing it,
|
||||||
// or the netstring doesn't end with a comma
|
// or the netstring doesn't end with a comma
|
||||||
if (self->name.len || c != ',')
|
if (self->name.len || c != ',')
|
||||||
{
|
return error_set (e, "invalid header netstring");
|
||||||
error_set (e, "invalid header netstring");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
self->state = SCGI_READING_CONTENT;
|
self->state = SCGI_READING_CONTENT;
|
||||||
keep_running = self->on_headers_read (self->user_data);
|
keep_running = self->on_headers_read (self->user_data);
|
||||||
}
|
}
|
||||||
@ -728,8 +718,7 @@ scgi_parser_push (struct scgi_parser *self,
|
|||||||
if (!self->headers_len)
|
if (!self->headers_len)
|
||||||
{
|
{
|
||||||
// The netstring is ending but we haven't finished parsing it
|
// The netstring is ending but we haven't finished parsing it
|
||||||
error_set (e, "invalid header netstring");
|
return error_set (e, "invalid header netstring");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
else if (c != '\0')
|
else if (c != '\0')
|
||||||
str_append_c (&self->value, c);
|
str_append_c (&self->value, c);
|
||||||
|
31
liberty.c
31
liberty.c
@ -633,14 +633,14 @@ struct error
|
|||||||
char *message; ///< Textual description of the event
|
char *message; ///< Textual description of the event
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static bool
|
||||||
error_set (struct error **e, const char *message, ...) ATTRIBUTE_PRINTF (2, 3);
|
error_set (struct error **e, const char *message, ...) ATTRIBUTE_PRINTF (2, 3);
|
||||||
|
|
||||||
static void
|
static bool
|
||||||
error_set (struct error **e, const char *message, ...)
|
error_set (struct error **e, const char *message, ...)
|
||||||
{
|
{
|
||||||
if (!e)
|
if (!e)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start (ap, message);
|
va_start (ap, message);
|
||||||
@ -660,6 +660,7 @@ error_set (struct error **e, const char *message, ...)
|
|||||||
|
|
||||||
soft_assert (*e == NULL);
|
soft_assert (*e == NULL);
|
||||||
*e = tmp;
|
*e = tmp;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -725,10 +726,7 @@ random_bytes (void *output, size_t len, struct error **e)
|
|||||||
ssize_t got = 0;
|
ssize_t got = 0;
|
||||||
|
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
return error_set (e, "%s: %s", "open", strerror (errno));
|
||||||
error_set (e, "%s: %s", "open", strerror (errno));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else if ((got = read (fd, output, len)) < 0)
|
else if ((got = read (fd, output, len)) < 0)
|
||||||
error_set (e, "%s: %s", "read", strerror (errno));
|
error_set (e, "%s: %s", "read", strerror (errno));
|
||||||
else if (got != (ssize_t) len)
|
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))
|
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));
|
path, strerror (errno));
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!S_ISDIR (st.st_mode))
|
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");
|
path, "file exists but is not a directory");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -3425,9 +3421,8 @@ read_file (const char *filename, struct str *output, struct error **e)
|
|||||||
FILE *fp = fopen (filename, "rb");
|
FILE *fp = fopen (filename, "rb");
|
||||||
if (!fp)
|
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));
|
filename, strerror (errno));
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
@ -3443,9 +3438,8 @@ read_file (const char *filename, struct str *output, struct error **e)
|
|||||||
if (success)
|
if (success)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
error_set (e, "error while reading `%s': %s",
|
return error_set (e, "error while reading `%s': %s",
|
||||||
filename, strerror (errno));
|
filename, strerror (errno));
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Overwrites filename contents with data; creates directories as needed
|
/// 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");
|
FILE *fp = fopen (filename, "w");
|
||||||
if (!fp)
|
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));
|
filename, strerror (errno));
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fwrite (data, data_len, 1, fp);
|
fwrite (data, data_len, 1, fp);
|
||||||
@ -3474,8 +3467,8 @@ write_file (const char *filename, const void *data, size_t data_len,
|
|||||||
|
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
error_set (e, "writing to `%s' failed: %s", filename, strerror (errno));
|
return error_set (e, "writing to `%s' failed: %s",
|
||||||
return false;
|
filename, strerror (errno));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user