Skip to content

Commit

Permalink
confile: cleanup set_config_console_buffer_size()
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Dec 8, 2020
1 parent 9a26e4a commit 3f5c01d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/lxc/confile.c
Expand Up @@ -2350,29 +2350,26 @@ static int set_config_console_buffer_size(const char *key, const char *value,
}

ret = parse_byte_size_string(value, &size);
if (ret < 0)
return -1;
if (ret)
return ret;

if (size < 0)
return -EINVAL;
return ret_errno(EINVAL);

/* must be at least a page size */
pgsz = lxc_getpagesize();
if ((uint64_t)size < pgsz) {
NOTICE("Requested ringbuffer size for the console is %" PRId64
" but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64 " bytes",
NOTICE("Requested ringbuffer size for the console is %" PRId64 " but must be at least %" PRId64 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
size, pgsz, pgsz);
size = pgsz;
}

buffer_size = lxc_find_next_power2((uint64_t)size);
if (buffer_size == 0)
return -EINVAL;
return ret_errno(EINVAL);

if (buffer_size != size)
NOTICE("Passed size was not a power of 2. Rounding log size to "
"next power of two: %" PRIu64 " bytes", buffer_size);
NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64 " bytes", buffer_size);

lxc_conf->console.buffer_size = buffer_size;

Expand Down
16 changes: 8 additions & 8 deletions src/lxc/string_utils.c
Expand Up @@ -907,21 +907,21 @@ int parse_byte_size_string(const char *s, int64_t *converted)
char suffix[3] = {0};

if (!s || !strcmp(s, ""))
return -EINVAL;
return ret_errno(EINVAL);

end = stpncpy(dup, s, sizeof(dup) - 1);
if (*end != '\0')
return -EINVAL;
return ret_errno(EINVAL);

if (isdigit(*(end - 1)))
suffix_len = 0;
else if (isalpha(*(end - 1)))
suffix_len = 1;
else
return -EINVAL;
return ret_errno(EINVAL);

if (suffix_len > 0 && (end - 2) == dup && !isdigit(*(end - 2)))
return -EINVAL;
return ret_errno(EINVAL);

if (suffix_len > 0 && isalpha(*(end - 2)))
suffix_len++;
Expand All @@ -934,8 +934,8 @@ int parse_byte_size_string(const char *s, int64_t *converted)
dup[lxc_char_right_gc(dup, strlen(dup))] = '\0';

ret = lxc_safe_long_long(dup, &conv);
if (ret < 0)
return -ret;
if (ret)
return ret;

if (suffix_len != 2) {
*converted = conv;
Expand All @@ -949,11 +949,11 @@ int parse_byte_size_string(const char *s, int64_t *converted)
else if (strcasecmp(suffix, "GB") == 0)
mltpl = 1024 * 1024 * 1024;
else
return -EINVAL;
return ret_errno(EINVAL);

overflow = conv * mltpl;
if (conv != 0 && (overflow / conv) != mltpl)
return -ERANGE;
return ret_errno(ERANGE);

*converted = overflow;
return 0;
Expand Down

0 comments on commit 3f5c01d

Please sign in to comment.