Skip to content

Commit

Permalink
Merge pull request #145 from tlsa/tlsa/minor-improvements
Browse files Browse the repository at this point in the history
Tlsa/minor improvements
  • Loading branch information
tlsa committed Nov 18, 2020
2 parents c95c7bc + 8d73022 commit 031f65b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,9 @@ static cyaml_err_t cyaml__read_int(

if (end == value || errno == ERANGE ||
temp < min || temp > max) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Invalid INT value: '%s'\n",
value);
return CYAML_ERR_INVALID_VALUE;
}

Expand All @@ -1324,11 +1327,13 @@ static cyaml_err_t cyaml__read_int(
/**
* Helper to read a number into a uint64_t.
*
* \param[in] ctx The CYAML loading context.
* \param[in] value String containing scaler value.
* \param[in] out The place to write the value in the output data.
* \return \ref CYAML_OK on success, or appropriate error code otherwise.
*/
static inline cyaml_err_t cyaml__read_uint64_t(
const cyaml_ctx_t *ctx,
const char *value,
uint64_t *out)
{
Expand All @@ -1339,6 +1344,9 @@ static inline cyaml_err_t cyaml__read_uint64_t(
temp = strtoull(value, &end, 0);

if (end == value || errno == ERANGE) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Invalid uint64_t value: '%s'\n",
value);
return CYAML_ERR_INVALID_VALUE;
}

Expand Down Expand Up @@ -1371,13 +1379,16 @@ static cyaml_err_t cyaml__read_uint(
return CYAML_ERR_INVALID_DATA_SIZE;
}

err = cyaml__read_uint64_t(value, &temp);
err = cyaml__read_uint64_t(ctx, value, &temp);
if (err != CYAML_OK) {
return err;
}

max = (~(uint64_t)0) >> ((8 - schema->data_size) * 8);
if (temp > max) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Invalid UINT value: '%s'\n",
value);
return CYAML_ERR_INVALID_VALUE;
}

Expand All @@ -1401,7 +1412,7 @@ static cyaml_err_t cyaml__read_bool(
{
bool temp = true;
static const char * const false_strings[] = {
"false", "no", "disable", "0",
"false", "no", "off", "disable", "0",
};

CYAML_UNUSED(ctx);
Expand Down Expand Up @@ -1443,11 +1454,14 @@ static cyaml_err_t cyaml__read_enum(

if (schema->flags & CYAML_FLAG_STRICT) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Invalid enumeration value: %s\n", value);
"Load: Invalid ENUM value: %s\n", value);
return CYAML_ERR_INVALID_VALUE;

}

cyaml__log(ctx->config, CYAML_LOG_DEBUG,
"Load: Attempt numerical fallback for ENUM: "
"'%s'\n", value);
return cyaml__read_int(ctx, schema, value, data);
}

Expand Down Expand Up @@ -1478,6 +1492,8 @@ static cyaml_err_t cyaml__read_float_f(
temp = strtof(value, &end);

if (end == value || errno == ERANGE) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Invalid FLOAT value: %s\n", value);
return CYAML_ERR_INVALID_VALUE;
}

Expand Down Expand Up @@ -1513,6 +1529,8 @@ static cyaml_err_t cyaml__read_float_d(
temp = strtod(value, &end);

if (end == value || errno == ERANGE) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Invalid FLOAT value: %s\n", value);
return CYAML_ERR_INVALID_VALUE;
}

Expand Down Expand Up @@ -1594,8 +1612,14 @@ static cyaml_err_t cyaml__read_string(
if (schema->string.min > schema->string.max) {
return CYAML_ERR_BAD_MIN_MAX_SCHEMA;
} else if (str_len < schema->string.min) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: STRING length < %"PRIu32": %s\n",
schema->string.min, value);
return CYAML_ERR_STRING_LENGTH_MIN;
} else if (str_len > schema->string.max) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: STRING length > %"PRIu32": %s\n",
schema->string.max, value);
return CYAML_ERR_STRING_LENGTH_MAX;
}

Expand Down Expand Up @@ -1821,7 +1845,7 @@ static cyaml_err_t cyaml__set_bitval(

switch (cyaml__get_event_type(event)) {
case CYAML_EVT_SCALAR:
err = cyaml__read_uint64_t(
err = cyaml__read_uint64_t(ctx,
(const char *)event->data.scalar.value, &value);
if (err != CYAML_OK) {
return err;
Expand Down
61 changes: 61 additions & 0 deletions test/units/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,66 @@ static bool test_load_mapping_entry_enum_sparse(
return ttest_pass(&tc);
}

/**
* Test loading an enumeration with numerical fallback.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_load_mapping_entry_enum_fallback(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
enum test_enum {
TEST_ENUM_FIRST = 3,
TEST_ENUM_SECOND = 77,
TEST_ENUM_THIRD = 183,
TEST_ENUM_FOURTH = 9900,
} value = TEST_ENUM_SECOND;
static const cyaml_strval_t strings[] = {
{ "first", TEST_ENUM_FIRST },
{ "second", TEST_ENUM_SECOND },
{ "third", TEST_ENUM_THIRD },
{ "fourth", TEST_ENUM_FOURTH },
};
static const unsigned char yaml[] =
"test_enum: 77\n";
struct target_struct {
enum test_enum test_value_enum;
} *data_tgt = NULL;
static const struct cyaml_schema_field mapping_schema[] = {
CYAML_FIELD_ENUM("test_enum", CYAML_FLAG_DEFAULT,
struct target_struct, test_value_enum,
strings, CYAML_ARRAY_LEN(strings)),
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.data = (cyaml_data_t **) &data_tgt,
.config = config,
.schema = &top_schema,
};
cyaml_err_t err;

ttest_ctx_t tc = ttest_start(report, __func__, cyaml_cleanup, &td);

err = cyaml_load_data(yaml, YAML_LEN(yaml), config, &top_schema,
(cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_OK) {
return ttest_fail(&tc, cyaml_strerror(err));
}

if (data_tgt->test_value_enum != value) {
return ttest_fail(&tc, "Incorrect value");
}

return ttest_pass(&tc);
}

/**
* Test loading a string to a character array.
*
Expand Down Expand Up @@ -6619,6 +6679,7 @@ bool load_tests(
pass &= test_load_mapping_entry_enum_sparse(rc, &config);
pass &= test_load_mapping_entry_ignore_deep(rc, &config);
pass &= test_load_mapping_entry_ignore_scalar(rc, &config);
pass &= test_load_mapping_entry_enum_fallback(rc, &config);
pass &= test_load_mapping_entry_bool_true_ptr(rc, &config);
pass &= test_load_mapping_entry_bool_false_ptr(rc, &config);
pass &= test_load_mapping_entry_string_ptr_empty(rc, &config);
Expand Down

0 comments on commit 031f65b

Please sign in to comment.