confile_utils: cleanup parse_idmaps()

parent 29bde371
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "log.h" #include "log.h"
#include "lxccontainer.h" #include "lxccontainer.h"
#include "macro.h" #include "macro.h"
#include "memory_utils.h"
#include "network.h" #include "network.h"
#include "parse.h" #include "parse.h"
#include "utils.h" #include "utils.h"
...@@ -31,16 +32,16 @@ lxc_log_define(confile_utils, lxc); ...@@ -31,16 +32,16 @@ lxc_log_define(confile_utils, lxc);
int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
unsigned long *hostid, unsigned long *range) unsigned long *hostid, unsigned long *range)
{ {
__do_free char *dup = NULL;
int ret = -1; int ret = -1;
unsigned long tmp_hostid, tmp_nsid, tmp_range; unsigned long tmp_hostid, tmp_nsid, tmp_range;
char tmp_type; char tmp_type;
char *window, *slide; char *window, *slide;
char *dup = NULL;
/* Duplicate string. */ /* Duplicate string. */
dup = strdup(idmap); dup = strdup(idmap);
if (!dup) if (!dup)
goto on_error; return ret_errno(ENOMEM);
/* A prototypical idmap entry would be: "u 1000 1000000 65536" */ /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
...@@ -49,13 +50,11 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -49,13 +50,11 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
/* skip whitespace */ /* skip whitespace */
slide += strspn(slide, " \t\r"); slide += strspn(slide, " \t\r");
if (slide != window && *slide == '\0') if (slide != window && *slide == '\0')
goto on_error; return ret_errno(EINVAL);
/* Validate type. */ /* Validate type. */
if (*slide != 'u' && *slide != 'g') { if (*slide != 'u' && *slide != 'g')
ERROR("Invalid id mapping type: %c", *slide); return log_error_errno(-EINVAL, EINVAL, "Invalid id mapping type: %c", *slide);
goto on_error;
}
/* Assign type. */ /* Assign type. */
tmp_type = *slide; tmp_type = *slide;
...@@ -68,7 +67,7 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -68,7 +67,7 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
slide += strspn(slide, " \t\r"); slide += strspn(slide, " \t\r");
/* There must be whitespace. */ /* There must be whitespace. */
if (slide == window) if (slide == window)
goto on_error; return ret_errno(EINVAL);
/* Mark beginning of nsid. */ /* Mark beginning of nsid. */
window = slide; window = slide;
...@@ -76,15 +75,14 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -76,15 +75,14 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
slide += strcspn(slide, " \t\r"); slide += strcspn(slide, " \t\r");
/* There must be non-whitespace. */ /* There must be non-whitespace. */
if (slide == window || *slide == '\0') if (slide == window || *slide == '\0')
goto on_error; return ret_errno(EINVAL);
/* Mark end of nsid. */ /* Mark end of nsid. */
*slide = '\0'; *slide = '\0';
/* Parse nsid. */ /* Parse nsid. */
if (lxc_safe_ulong(window, &tmp_nsid) < 0) { ret = lxc_safe_ulong(window, &tmp_nsid);
ERROR("Failed to parse nsid: %s", window); if (ret < 0)
goto on_error; return log_error_errno(ret, errno, "Failed to parse nsid: %s", window);
}
/* Move beyond \0. */ /* Move beyond \0. */
slide++; slide++;
...@@ -94,7 +92,7 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -94,7 +92,7 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
* So only ensure that we're not at the end of the string. * So only ensure that we're not at the end of the string.
*/ */
if (*slide == '\0') if (*slide == '\0')
goto on_error; return ret_errno(EINVAL);
/* Mark beginning of hostid. */ /* Mark beginning of hostid. */
window = slide; window = slide;
...@@ -102,15 +100,14 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -102,15 +100,14 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
slide += strcspn(slide, " \t\r"); slide += strcspn(slide, " \t\r");
/* There must be non-whitespace. */ /* There must be non-whitespace. */
if (slide == window || *slide == '\0') if (slide == window || *slide == '\0')
goto on_error; return ret_errno(EINVAL);
/* Mark end of nsid. */ /* Mark end of nsid. */
*slide = '\0'; *slide = '\0';
/* Parse hostid. */ /* Parse hostid. */
if (lxc_safe_ulong(window, &tmp_hostid) < 0) { ret = lxc_safe_ulong(window, &tmp_hostid);
ERROR("Failed to parse hostid: %s", window); if (ret < 0)
goto on_error; return log_error_errno(ret, errno, "Failed to parse hostid: %s", window);
}
/* Move beyond \0. */ /* Move beyond \0. */
slide++; slide++;
...@@ -120,7 +117,7 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -120,7 +117,7 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
* So only ensure that we're not at the end of the string. * So only ensure that we're not at the end of the string.
*/ */
if (*slide == '\0') if (*slide == '\0')
goto on_error; return ret_errno(EINVAL);
/* Mark beginning of range. */ /* Mark beginning of range. */
window = slide; window = slide;
...@@ -128,35 +125,29 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -128,35 +125,29 @@ int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
slide += strcspn(slide, " \t\r"); slide += strcspn(slide, " \t\r");
/* There must be non-whitespace. */ /* There must be non-whitespace. */
if (slide == window) if (slide == window)
goto on_error; return ret_errno(EINVAL);
/* The range is the last valid entry we expect. So make sure that there /* The range is the last valid entry we expect. So make sure that there
* is no trailing garbage and if there is, error out. * is no trailing garbage and if there is, error out.
*/ */
if (*(slide + strspn(slide, " \t\r\n")) != '\0') if (*(slide + strspn(slide, " \t\r\n")) != '\0')
goto on_error; return ret_errno(EINVAL);
/* Mark end of range. */ /* Mark end of range. */
*slide = '\0'; *slide = '\0';
/* Parse range. */ /* Parse range. */
if (lxc_safe_ulong(window, &tmp_range) < 0) { ret = lxc_safe_ulong(window, &tmp_range);
ERROR("Failed to parse id mapping range: %s", window); if (ret < 0)
goto on_error; return log_error_errno(ret, errno, "Failed to parse id mapping range: %s", window);
}
*type = tmp_type; *type = tmp_type;
*nsid = tmp_nsid; *nsid = tmp_nsid;
*hostid = tmp_hostid; *hostid = tmp_hostid;
*range = tmp_range; *range = tmp_range;
/* Yay, we survived. */ /* Yay, we survived. */
ret = 0; return 0;
on_error:
free(dup);
return ret;
} }
bool lxc_config_value_empty(const char *value) bool lxc_config_value_empty(const char *value)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment