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:
parent
0f12ee87ac
commit
31aa43d258
4 changed files with 69 additions and 38 deletions
|
@ -1997,6 +1997,63 @@ exit:
|
|||
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)
|
||||
{
|
||||
*sort = cil_malloc(sizeof(**sort));
|
||||
|
|
|
@ -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 rc = SEPOL_ERR;
|
||||
char *endptr = NULL;
|
||||
unsigned long val;
|
||||
|
||||
if (int_node == NULL || int_node->data == NULL || integer == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
val = strtoul(int_node->data, &endptr, base);
|
||||
if (errno != 0 || endptr == int_node->data || *endptr != '\0') {
|
||||
rc = SEPOL_ERR;
|
||||
rc = cil_string_to_uint32(int_node->data, integer, base);
|
||||
if (rc != SEPOL_OK) {
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int cil_fill_integer64(struct cil_tree_node *int_node, uint64_t *integer, int base)
|
||||
{
|
||||
int rc = SEPOL_ERR;
|
||||
char *endptr = NULL;
|
||||
uint64_t val;
|
||||
|
||||
if (int_node == NULL || int_node->data == NULL || integer == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
val = strtoull(int_node->data, &endptr, base);
|
||||
if (errno != 0 || endptr == int_node->data || *endptr != '\0') {
|
||||
rc = SEPOL_ERR;
|
||||
rc = cil_string_to_uint64(int_node->data, integer, base);
|
||||
if (rc != SEPOL_OK) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
*integer = val;
|
||||
|
||||
return SEPOL_OK;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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_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_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_destroy(struct cil_sort **sort);
|
||||
|
|
|
@ -103,8 +103,7 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
|
|||
struct cil_tree_node *node;
|
||||
struct token tok;
|
||||
char *hll_file;
|
||||
char *end = NULL;
|
||||
unsigned long val;
|
||||
int rc;
|
||||
|
||||
cil_lexer_next(&tok);
|
||||
if (tok.type != SYMBOL) {
|
||||
|
@ -142,18 +141,11 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
|
|||
goto exit;
|
||||
}
|
||||
|
||||
val = strtoul(tok.value, &end, 10);
|
||||
if (errno == ERANGE || *end != '\0') {
|
||||
cil_log(CIL_ERR, "Problem parsing line number for line mark\n");
|
||||
rc = cil_string_to_uint32(tok.value, hll_lineno, 10);
|
||||
if (rc != SEPOL_OK) {
|
||||
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;
|
||||
|
||||
cil_lexer_next(&tok);
|
||||
|
|
Loading…
Reference in a new issue