Browse Source

Simplify error handling in the secretcfg module

Apply the same logic as for the secret module: abort on memory
allocation error, use assert to check for pre-conditions.
master
Damien Goutte-Gattat 5 years ago
parent
commit
c06508bfd7
  1. 84
      src/secretcfg.c

84
src/secretcfg.c

@ -25,6 +25,9 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <xmem.h>
#define MAX_LINE_LENGTH 256
@ -70,25 +73,21 @@ get_line(FILE *f, char *buffer, size_t len)
* @param uri The address of the URI string. The pointer is
* advanced to the '/' character separating the
* authority from the path.
* @param autorithy The address of a newly allocated buffer to store
* the authority.
*
* @return 0 if successfull, or one of the GFSEC_ERR_* error codes.
* @return A newly-allocated string containing the authority, or
* NULL if the uri did not contain a '/' character.
*/
static int
parse_authority(const char **uri, char **authority)
static char *
parse_authority(const char **uri)
{
char *slash;
char *slash, *authority = NULL;
if ( (slash = strchr(*uri, '/')) ) {
if ( ! (*authority = strndup(*uri, slash - *uri)) )
return GFSEC_ERR_SYSTEM_ERROR;
authority = xstrndup(*uri, slash - *uri);
*uri = slash;
return 0;
}
else
return GFSEC_ERR_CONFIG_INVALID_URI;
return authority;
}
/**
@ -99,25 +98,19 @@ parse_authority(const char **uri, char **authority)
* @param uri The address of the URI string. The pointer is advanced
* to the first '?' character, or to the terminating NULL
* byte.
* @param path The address of a newly allocated buffer to store the
* extracted path.
*
* @return 0 if the path was successfully parsed, or one of
* the GFSEC_ERR_* error codes.
* @return A newly-allocated buffer containing the path.
*/
static int
parse_path(const char **uri, char **path)
static char *
parse_path(const char **uri)
{
char *qm;
char *qm, *path;
qm = strchrnul(*uri, '?');
if ( ! (*path = strndup(*uri, qm - *uri)) )
return GFSEC_ERR_SYSTEM_ERROR;
path = xstrndup(*uri, qm - *uri);
*uri = qm;
return 0;
return path;
}
/**
@ -129,7 +122,8 @@ parse_path(const char **uri, char **path)
* the hash value.
*
* @return 0 if the hash value was successfully parsed,
* or one of the GFSEC_ERR_* error codes.
* or GFSEC_ERR_CONFIG_INVALID_HASH if the string did not contain
* a valid SHA-256 hash value.
*/
static int
parse_sha256(const char *hex, size_t len, unsigned char **sha256)
@ -139,8 +133,7 @@ parse_sha256(const char *hex, size_t len, unsigned char **sha256)
if ( len != 64 )
return GFSEC_ERR_CONFIG_INVALID_HASH;
if ( ! (*sha256 = malloc(32)) )
return GFSEC_ERR_SYSTEM_ERROR;
*sha256 = xmalloc(32);
for ( n = 0; n < len; n++ ) {
unsigned char h, val;
@ -266,11 +259,10 @@ gfsec_parse_uri(const char *uri, gfsec_secret_t *secret, int assign)
const char *p = uri;
int rc = 0;
if ( ! uri || ! secret )
return GFSEC_ERR_INVALID_CALL;
assert(uri != NULL);
assert(secret != NULL);
if ( ! (share = gfsec_share_new()) )
return GFSEC_ERR_SYSTEM_ERROR;
share = gfsec_share_new();
if ( strncmp(p, "file://", 7) == 0 && (p += 7) )
share->scheme = GFSEC_SCHEME_FILE;
@ -283,11 +275,11 @@ gfsec_parse_uri(const char *uri, gfsec_secret_t *secret, int assign)
else
rc = GFSEC_ERR_CONFIG_UNKNOWN_SCHEME;
if ( rc == 0 )
rc = parse_authority(&p, &(share->authority));
if ( rc == 0 && ! (share->authority = parse_authority(&p)) )
rc = GFSEC_ERR_CONFIG_INVALID_URI;
if ( rc == 0 )
rc = parse_path(&p, &(share->path));
share->path = parse_path(&p);
while ( rc == 0 && *p )
rc = parse_parameter(&p, share);
@ -320,9 +312,8 @@ gfsec_parse_uri(const char *uri, gfsec_secret_t *secret, int assign)
*
* @return
* - 0 if successful;
* - GFSEC_ERR_INVALID_CALL if an invalid pointer was passed;
* - GFSEC_ERR_SYSTEM_ERROR if an error occured when reading
* the file or allocating memory (check errno for details);
* the file;
* - GFSEC_ERR_TOO_MANY_SHARES if the configuration describes
* a secret with too many shares (more than 255);
* - one of the GFSEC_ERR_CONFIG_* error codes if the syntax
@ -337,17 +328,13 @@ gfsec_read_config(gfsec_secret_t **cfg,
char buffer[MAX_LINE_LENGTH];
int rc;
if ( ! cfg || ! filename )
return GFSEC_ERR_INVALID_CALL;
assert(cfg != NULL);
assert(filename != NULL);
if ( ! (f = fopen(filename, "r")) )
return GFSEC_ERR_SYSTEM_ERROR;
if ( ! (*cfg = gfsec_secret_new()) ) {
fclose(f);
return GFSEC_ERR_SYSTEM_ERROR;
}
*cfg = gfsec_secret_new();
(*cfg)->threshold = 2;
if ( line )
@ -367,10 +354,8 @@ gfsec_read_config(gfsec_secret_t **cfg,
rc = n; /* Abort on any other error. */
else if ( n == 0 || buffer[0] == '#' )
; /* Comment or empty line */
else if ( strncmp("OUTFILE=", buffer, 8) == 0 ) {
if ( ! ((*cfg)->filename = strdup(buffer + 8)) )
rc = GFSEC_ERR_SYSTEM_ERROR;
}
else if ( strncmp("OUTFILE=", buffer, 8) == 0 )
(*cfg)->filename = xstrdup(buffer + 8);
else if ( strncmp("MINSHARES=", buffer, 10) == 0 ) {
char *endptr;
unsigned long l;
@ -403,7 +388,6 @@ gfsec_read_config(gfsec_secret_t **cfg,
*
* @return
* - 0 if successful;
* - GFSEC_ERR_INVALID_CALL if an invalid pointer was passed;
* - GFSEC_ERR_SYSTEM_ERROR if an I/O error occured.
*/
int
@ -415,8 +399,8 @@ gfsec_write_config(gfsec_secret_t *cfg, const char *filename)
static char *schemes[] = { "file://", "uuid://", "label://", "mtp://" };
if ( ! cfg || ! filename )
return GFSEC_ERR_INVALID_CALL;
assert(cfg != NULL);
assert(filename != NULL);
if ( ! (f = fopen(filename, "w")) )
return GFSEC_ERR_SYSTEM_ERROR;

Loading…
Cancel
Save