libsepol/cil: Create common string-to-unsigned-integer functions

The functions cil_fill_integer() and cil_fill_integer64() exist in
cil_build_ast.c, but these functions take a node and it would be
better to have a function that can be used in add_hll_linemark()
so that the common functinality is in one place.

Create cil_string_to_uint32() and cil_string_to_uint64() and use
these functions in cil_fill_integer(), cil_fill_integer64(), and
add_hll_linemark().

Signed-off-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
James Carter 2021-08-16 13:44:25 -04:00
parent 0f12ee87ac
commit 31aa43d258
4 changed files with 69 additions and 38 deletions

View file

@ -1997,6 +1997,63 @@ exit:
return SEPOL_ERR; return SEPOL_ERR;
} }
int cil_string_to_uint32(const char *string, uint32_t *value, int base)
{
unsigned long val;
char *end = NULL;
int rc = SEPOL_ERR;
if (string == NULL || value == NULL) {
goto exit;
}
errno = 0;
val = strtoul(string, &end, base);
if (errno != 0 || end == string || *end != '\0') {
rc = SEPOL_ERR;
goto exit;
}
/* Ensure that the value fits a 32-bit integer without triggering -Wtype-limits */
#if ULONG_MAX > UINT32_MAX
if (val > UINT32_MAX) {
rc = SEPOL_ERR;
goto exit;
}
#endif
*value = val;
return SEPOL_OK;
exit:
cil_log(CIL_ERR, "Failed to create uint32_t from string\n");
return rc;
}
int cil_string_to_uint64(const char *string, uint64_t *value, int base)
{
char *end = NULL;
int rc = SEPOL_ERR;
if (string == NULL || value == NULL) {
goto exit;
}
errno = 0;
*value = strtoull(string, &end, base);
if (errno != 0 || end == string || *end != '\0') {
rc = SEPOL_ERR;
goto exit;
}
return SEPOL_OK;
exit:
cil_log(CIL_ERR, "Failed to create uint64_t from string\n");
return rc;
}
void cil_sort_init(struct cil_sort **sort) void cil_sort_init(struct cil_sort **sort)
{ {
*sort = cil_malloc(sizeof(**sort)); *sort = cil_malloc(sizeof(**sort));

View file

@ -5601,60 +5601,40 @@ void cil_destroy_ipaddr(struct cil_ipaddr *ipaddr)
int cil_fill_integer(struct cil_tree_node *int_node, uint32_t *integer, int base) int cil_fill_integer(struct cil_tree_node *int_node, uint32_t *integer, int base)
{ {
int rc = SEPOL_ERR; int rc = SEPOL_ERR;
char *endptr = NULL;
unsigned long val;
if (int_node == NULL || int_node->data == NULL || integer == NULL) { if (int_node == NULL || int_node->data == NULL || integer == NULL) {
goto exit; goto exit;
} }
errno = 0; rc = cil_string_to_uint32(int_node->data, integer, base);
val = strtoul(int_node->data, &endptr, base); if (rc != SEPOL_OK) {
if (errno != 0 || endptr == int_node->data || *endptr != '\0') {
rc = SEPOL_ERR;
goto exit; goto exit;
} }
/* Ensure that the value fits a 32-bit integer without triggering -Wtype-limits */
#if ULONG_MAX > UINT32_MAX
if (val > UINT32_MAX) {
rc = SEPOL_ERR;
goto exit;
}
#endif
*integer = val;
return SEPOL_OK; return SEPOL_OK;
exit: exit:
cil_log(CIL_ERR, "Failed to create integer from string\n"); cil_log(CIL_ERR, "Failed to fill 32-bit integer\n");
return rc; return rc;
} }
int cil_fill_integer64(struct cil_tree_node *int_node, uint64_t *integer, int base) int cil_fill_integer64(struct cil_tree_node *int_node, uint64_t *integer, int base)
{ {
int rc = SEPOL_ERR; int rc = SEPOL_ERR;
char *endptr = NULL;
uint64_t val;
if (int_node == NULL || int_node->data == NULL || integer == NULL) { if (int_node == NULL || int_node->data == NULL || integer == NULL) {
goto exit; goto exit;
} }
errno = 0; rc = cil_string_to_uint64(int_node->data, integer, base);
val = strtoull(int_node->data, &endptr, base); if (rc != SEPOL_OK) {
if (errno != 0 || endptr == int_node->data || *endptr != '\0') {
rc = SEPOL_ERR;
goto exit; goto exit;
} }
*integer = val;
return SEPOL_OK; return SEPOL_OK;
exit: exit:
cil_log(CIL_ERR, "Failed to create integer from string\n"); cil_log(CIL_ERR, "Failed to fill 64-bit integer\n");
return rc; return rc;
} }

View file

@ -986,6 +986,8 @@ void cil_symtab_array_init(symtab_t symtab[], const int symtab_sizes[CIL_SYM_NUM
void cil_symtab_array_destroy(symtab_t symtab[]); void cil_symtab_array_destroy(symtab_t symtab[]);
void cil_destroy_ast_symtabs(struct cil_tree_node *root); void cil_destroy_ast_symtabs(struct cil_tree_node *root);
int cil_get_symtab(struct cil_tree_node *ast_node, symtab_t **symtab, enum cil_sym_index sym_index); int cil_get_symtab(struct cil_tree_node *ast_node, symtab_t **symtab, enum cil_sym_index sym_index);
int cil_string_to_uint32(const char *string, uint32_t *value, int base);
int cil_string_to_uint64(const char *string, uint64_t *value, int base);
void cil_sort_init(struct cil_sort **sort); void cil_sort_init(struct cil_sort **sort);
void cil_sort_destroy(struct cil_sort **sort); void cil_sort_destroy(struct cil_sort **sort);

View file

@ -103,8 +103,7 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
struct cil_tree_node *node; struct cil_tree_node *node;
struct token tok; struct token tok;
char *hll_file; char *hll_file;
char *end = NULL; int rc;
unsigned long val;
cil_lexer_next(&tok); cil_lexer_next(&tok);
if (tok.type != SYMBOL) { if (tok.type != SYMBOL) {
@ -142,18 +141,11 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
goto exit; goto exit;
} }
val = strtoul(tok.value, &end, 10); rc = cil_string_to_uint32(tok.value, hll_lineno, 10);
if (errno == ERANGE || *end != '\0') { if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Problem parsing line number for line mark\n");
goto exit; goto exit;
} }
#if ULONG_MAX > UINT32_MAX
if (val > UINT32_MAX) {
cil_log(CIL_ERR, "Line mark line number > UINT32_MAX\n");
goto exit;
}
#endif
*hll_lineno = val;
*hll_expand = (hll_type == CIL_KEY_HLL_LMX) ? 1 : 0; *hll_expand = (hll_type == CIL_KEY_HLL_LMX) ? 1 : 0;
cil_lexer_next(&tok); cil_lexer_next(&tok);