Skip to content

Commit

Permalink
Load: Improve log message for UINT value out of range
Browse files Browse the repository at this point in the history
Now it matches the other out of range messages. It also uses the
string value from the YAML, rather than the uint_64t conversion,
which can look confusing when there's a negative number in the
YAML.
  • Loading branch information
tlsa committed Oct 2, 2024
1 parent c9956cb commit 96b5760
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ static inline cyaml_err_t cyaml__validate_uint(
*
* \param[in] ctx The CYAML loading context.
* \param[in] schema The schema for the value to be stored.
* \param[in] value_str The value to store's string form, or NULL if none.
* \param[in] location The place to write the value in the output data.
* \param[in] value The value to store.
* \param[in] validate Whether to validate the value before storing it.
Expand All @@ -313,6 +314,7 @@ static inline cyaml_err_t cyaml__validate_uint(
static inline cyaml_err_t cyaml__store_uint(
const cyaml_ctx_t *ctx,
const cyaml_schema_value_t *schema,
const char *value_str,
uint8_t *location,
uint64_t value,
bool validate)
Expand All @@ -329,9 +331,20 @@ static inline cyaml_err_t cyaml__store_uint(

max = (~(uint64_t)0) >> ((8 - schema->data_size) * 8);
if (value > max) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Invalid %s value: '%" PRIu64 "'\n",
cyaml__type_to_str(schema->type), value);
if (value_str != NULL) {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: %s value out of range: '%s'\n",
cyaml__type_to_str(
schema->type),
value_str);
} else {
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: %s value out of range: '%"
PRIu64 "'\n",
cyaml__type_to_str(
schema->type),
value);
}
return CYAML_ERR_INVALID_VALUE;
}

Expand Down Expand Up @@ -1375,13 +1388,13 @@ static cyaml_err_t cyaml__field_scalar_apply_default(
return cyaml__store_int(ctx, schema, data,
schema->enumeration.missing, false);
case CYAML_UINT:
return cyaml__store_uint(ctx, schema, data,
return cyaml__store_uint(ctx, schema, NULL, data,
schema->unsigned_integer.missing, false);
case CYAML_FLAGS:
return cyaml__store_uint(ctx, schema, data,
return cyaml__store_uint(ctx, schema, NULL, data,
(uint64_t)schema->enumeration.missing, false);
case CYAML_BITFIELD:
return cyaml__store_uint(ctx, schema, data,
return cyaml__store_uint(ctx, schema, NULL, data,
schema->bitfield.missing, false);
case CYAML_BOOL:
return cyaml__store_bool(ctx, schema, data,
Expand Down Expand Up @@ -1926,7 +1939,7 @@ static cyaml_err_t cyaml__read_uint(
return err;
}

return cyaml__store_uint(ctx, schema, data, temp, true);
return cyaml__store_uint(ctx, schema, value, data, temp, true);
}

/**
Expand Down Expand Up @@ -2211,7 +2224,7 @@ static cyaml_err_t cyaml__read_flags_value(
}
}

err = cyaml__store_uint(ctx, schema, data, value, true);
err = cyaml__store_uint(ctx, schema, NULL, data, value, true);
if (err != CYAML_OK) {
return err;
}
Expand Down Expand Up @@ -2360,7 +2373,7 @@ static cyaml_err_t cyaml__read_bitfield_value(
}
}

err = cyaml__store_uint(ctx, schema, data, value, true);
err = cyaml__store_uint(ctx, schema, NULL, data, value, true);
if (err != CYAML_OK) {
return err;
}
Expand Down

0 comments on commit 96b5760

Please sign in to comment.