/* Call generic handler */
if (parser->var_handler (ptr, remain, &dst, &dstlen, &need_free,
parser->var_data)) {
- *out_len = dstlen;
*found = true;
+ *out_len = dstlen;
+
if (need_free) {
free (dst);
}
}
if (endptr < end && endptr != start) {
+ p = endptr;
switch (*p) {
case 'm':
case 'M':
*/
static bool
ucl_parse_key (struct ucl_parser *parser, struct ucl_chunk *chunk,
- bool *next_key, bool *end_of_object)
+ bool *next_key, bool *end_of_object, bool *got_content)
{
const unsigned char *p, *c = NULL, *end, *t;
const char *key = NULL;
bool got_quote = false, got_eq = false, got_semicolon = false,
need_unescape = false, ucl_escape = false, var_expand = false,
- got_content = false, got_sep = false;
+ got_sep = false;
ucl_object_t *nobj;
ssize_t keylen;
p = chunk->pos;
- if (*p == '.') {
- /* It is macro actually */
- if (!(parser->flags & UCL_PARSER_DISABLE_MACRO)) {
- ucl_chunk_skipc (chunk, p);
- }
-
+ if (*p == '.' && !(parser->flags & UCL_PARSER_DISABLE_MACRO)) {
+ ucl_chunk_skipc (chunk, p);
parser->prev_state = parser->state;
parser->state = UCL_STATE_MACRO_NAME;
*end_of_object = false;
/* The first symbol */
c = p;
ucl_chunk_skipc (chunk, p);
- got_content = true;
+ *got_content = true;
}
else if (*p == '"') {
/* JSON style key */
c = p + 1;
got_quote = true;
- got_content = true;
+ *got_content = true;
ucl_chunk_skipc (chunk, p);
}
else if (*p == '}') {
*end_of_object = true;
return true;
}
- else if (*p == '.') {
+ else if (*p == '.' && !(parser->flags & UCL_PARSER_DISABLE_MACRO)) {
ucl_chunk_skipc (chunk, p);
parser->prev_state = parser->state;
parser->state = UCL_STATE_MACRO_NAME;
/* Parse the body of a key */
if (!got_quote) {
if (ucl_test_character (*p, UCL_CHARACTER_KEY)) {
- got_content = true;
+ *got_content = true;
ucl_chunk_skipc (chunk, p);
}
else if (ucl_test_character (*p, UCL_CHARACTER_KEY_SEP)) {
}
}
- if (p >= chunk->end && got_content) {
+ if (p >= chunk->end && *got_content) {
ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err);
return false;
}
- else if (!got_content) {
+ else if (!*got_content) {
return true;
}
*end_of_object = false;
case '{':
obj = ucl_parser_get_container (parser);
if (obj == NULL) {
+ parser->state = UCL_STATE_ERROR;
+ ucl_set_err(parser, UCL_ESYNTAX, "object value must be a part of an object",
+ &parser->err);
return false;
}
/* We have a new object */
case '[':
obj = ucl_parser_get_container (parser);
if (obj == NULL) {
+ parser->state = UCL_STATE_ERROR;
+ ucl_set_err(parser, UCL_ESYNTAX, "array value must be a part of an object",
+ &parser->err);
return false;
}
/* We have a new array */
break;
case '<':
obj = ucl_parser_get_container (parser);
+ if (obj == NULL) {
+ parser->state = UCL_STATE_ERROR;
+ ucl_set_err(parser, UCL_ESYNTAX, "multiline value must be a part of an object",
+ &parser->err);
+ return false;
+ }
/* We have something like multiline value, which must be <<[A-Z]+\n */
if (chunk->end - p > 3) {
if (memcmp (p, "<<", 2) == 0) {
obj = ucl_parser_get_container (parser);
}
+ if (obj == NULL) {
+ parser->state = UCL_STATE_ERROR;
+ ucl_set_err(parser, UCL_ESYNTAX, "value must be a part of an object",
+ &parser->err);
+ return false;
+ }
+
/* Parse atom */
if (ucl_test_character (*p, UCL_CHARACTER_VALUE_DIGIT_START)) {
if (!ucl_lex_number (parser, chunk, obj)) {
unsigned char *macro_escaped;
size_t macro_len = 0;
struct ucl_macro *macro = NULL;
- bool next_key = false, end_of_object = false, ret;
+ bool next_key = false, end_of_object = false, got_content = false, ret;
if (parser->top_obj == NULL) {
parser->state = UCL_STATE_INIT;
parser->state = UCL_STATE_ERROR;
return false;
}
- if (!ucl_parse_key (parser, chunk, &next_key, &end_of_object)) {
+
+ got_content = false;
+
+ if (!ucl_parse_key (parser, chunk, &next_key, &end_of_object, &got_content)) {
parser->prev_state = parser->state;
parser->state = UCL_STATE_ERROR;
return false;
return false;
}
}
- else {
+ else if (got_content) {
+ /* Do not switch state if we have not read any content */
parser->state = UCL_STATE_VALUE;
}
}
return false;
}
break;
+ case UCL_STATE_ERROR:
+ /* Already in the error state */
+ return false;
default:
ucl_set_err (parser, UCL_EINTERNAL,
"internal error: parser is in an unknown state", &parser->err);