Compare commits

...

2 Commits

  1. 10
      src/data.h
  2. 56
      src/load.c
  3. 12
      src/util.h

10
src/data.h

@ -58,8 +58,9 @@ static inline void cyaml_data_write_pointer( @@ -58,8 +58,9 @@ static inline void cyaml_data_write_pointer(
{
/* Refuse to build on platforms where sizeof pointer would
* lead to \ref CYAML_ERR_INVALID_DATA_SIZE. */
cyaml_static_assert(sizeof(char *) > 0);
cyaml_static_assert(sizeof(char *) <= sizeof(uint64_t));
static_assert(sizeof(char *) > 0, "Incompatible pointer size.");
static_assert(sizeof(char *) <= sizeof(uint64_t),
"Incompatible pointer size.");
CYAML_UNUSED(cyaml_data_write((uint64_t)ptr, sizeof(ptr), data_target));
@ -115,8 +116,9 @@ static inline uint8_t * cyaml_data_read_pointer( @@ -115,8 +116,9 @@ static inline uint8_t * cyaml_data_read_pointer(
/* Refuse to build on platforms where sizeof pointer would
* lead to \ref CYAML_ERR_INVALID_DATA_SIZE. */
cyaml_static_assert(sizeof(char *) > 0);
cyaml_static_assert(sizeof(char *) <= sizeof(uint64_t));
static_assert(sizeof(char *) > 0, "Incompatible pointer size.");
static_assert(sizeof(char *) <= sizeof(uint64_t),
"Incompatible pointer size.");
return (void *)cyaml_data_read(sizeof(char *), data, &err);
}

56
src/load.c

@ -639,7 +639,6 @@ static void cyaml__handle_replay( @@ -639,7 +639,6 @@ static void cyaml__handle_replay(
* object.
*
* \param[in] ctx The CYAML loading context.
* \return \ref CYAML_OK on success, or appropriate error otherwise.
*/
static void cyaml__delete_yaml_event(
cyaml_ctx_t *ctx)
@ -879,17 +878,20 @@ static cyaml_err_t cyaml__mapping_bitfieid_create( @@ -879,17 +878,20 @@ static cyaml_err_t cyaml__mapping_bitfieid_create(
cyaml_ctx_t *ctx,
cyaml_state_t *state)
{
cyaml_bitfield_t *bitfield;
unsigned count = state->mapping.fields_count;
size_t size = ((count + CYAML_BITFIELD_BITS - 1) / CYAML_BITFIELD_BITS)
* sizeof(*bitfield);
bitfield = cyaml__alloc(ctx->config, size, true);
if (bitfield == NULL) {
return CYAML_ERR_OOM;
}
if (count != 0) {
cyaml_bitfield_t *bitfield;
size_t size = ((count + CYAML_BITFIELD_BITS - 1) /
CYAML_BITFIELD_BITS) * sizeof(*bitfield);
state->mapping.fields_set = bitfield;
bitfield = cyaml__alloc(ctx->config, size, true);
if (bitfield == NULL) {
return CYAML_ERR_OOM;
}
state->mapping.fields_set = bitfield;
}
return CYAML_OK;
}
@ -1312,6 +1314,9 @@ static cyaml_err_t cyaml__read_int( @@ -1312,6 +1314,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;
}
@ -1321,11 +1326,13 @@ static cyaml_err_t cyaml__read_int( @@ -1321,11 +1326,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)
{
@ -1336,6 +1343,9 @@ static inline cyaml_err_t cyaml__read_uint64_t( @@ -1336,6 +1343,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;
}
@ -1368,13 +1378,16 @@ static cyaml_err_t cyaml__read_uint( @@ -1368,13 +1378,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;
}
@ -1398,7 +1411,7 @@ static cyaml_err_t cyaml__read_bool( @@ -1398,7 +1411,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);
@ -1440,11 +1453,14 @@ static cyaml_err_t cyaml__read_enum( @@ -1440,11 +1453,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);
}
@ -1472,9 +1488,11 @@ static cyaml_err_t cyaml__read_float_f( @@ -1472,9 +1488,11 @@ static cyaml_err_t cyaml__read_float_f(
CYAML_UNUSED(schema);
errno = 0;
temp = strtof(value, &end);
temp = (float) 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;
}
@ -1510,6 +1528,8 @@ static cyaml_err_t cyaml__read_float_d( @@ -1510,6 +1528,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;
}
@ -1591,8 +1611,14 @@ static cyaml_err_t cyaml__read_string( @@ -1591,8 +1611,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;
}
@ -1818,7 +1844,7 @@ static cyaml_err_t cyaml__set_bitval( @@ -1818,7 +1844,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;
@ -2206,7 +2232,7 @@ static cyaml_err_t cyaml__map_key( @@ -2206,7 +2232,7 @@ static cyaml_err_t cyaml__map_key(
cyaml_event_t cyaml_event;
if (!(ctx->config->flags &
CYAML_CFG_IGNORE_UNKNOWN_KEYS)) {
cyaml__log(ctx->config, CYAML_LOG_DEBUG,
cyaml__log(ctx->config, CYAML_LOG_ERROR,
"Load: Unexpected key: %s\n", key);
return CYAML_ERR_INVALID_KEY;
}

12
src/util.h

@ -15,14 +15,6 @@ @@ -15,14 +15,6 @@
#include "cyaml/cyaml.h"
#include "utf8.h"
/** Compile time assertion macro. */
#define cyaml_static_assert(e) \
{ \
enum { \
cyaml_static_assert_check = 1 / (!!(e)) \
}; \
}
/** Macro to squash unused variable compiler warnings. */
#define CYAML_UNUSED(_x) ((void)(_x))
@ -37,7 +29,7 @@ static inline bool cyaml__host_is_little_endian(void) @@ -37,7 +29,7 @@ static inline bool cyaml__host_is_little_endian(void)
{
static const uint16_t test = 1;
return ((uint8_t *) &test)[0] == 1;
return ((const uint8_t *) &test)[0] == 1;
}
/**
@ -129,7 +121,7 @@ static inline const char * cyaml__type_to_str(cyaml_type_e type) @@ -129,7 +121,7 @@ static inline const char * cyaml__type_to_str(cyaml_type_e type)
static inline void cyaml__log(
const cyaml_config_t *cfg,
cyaml_log_t level,
char *fmt, ...)
const char *fmt, ...)
{
if (level >= cfg->log_level && cfg->log_fn != NULL) {
va_list args;

Loading…
Cancel
Save