From 6fde93d600a1b7a0948ee046620743e1274f2ea1 Mon Sep 17 00:00:00 2001 From: Florian Weimer Date: Mon, 3 Feb 2014 16:48:01 -0800 Subject: [PATCH 01/21] yaml_stack_extend: guard against integer overflow --- src/api.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/api.c b/src/api.c index b1a8da0b..35a58c5b 100644 --- a/src/api.c +++ b/src/api.c @@ -117,7 +117,12 @@ yaml_string_join( YAML_DECLARE(int) yaml_stack_extend(void **start, void **top, void **end) { - void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2); + void *new_start; + + if ((char *)*end - (char *)*start >= INT_MAX / 2) + return 0; + + new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2); if (!new_start) return 0; From 0de3679552f55e0f3c1403e29c796a211ca84133 Mon Sep 17 00:00:00 2001 From: John Eckersberg Date: Mon, 3 Feb 2014 16:48:19 -0800 Subject: [PATCH 02/21] yaml_parser-{un,}roll-indent: fix int overflow in column argument --- src/scanner.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/src/scanner.c b/src/scanner.c index 1189d9db..b74b961b 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -621,6 +621,9 @@ yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column, static int yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column); +static int +yaml_parser_reset_indent(yaml_parser_t *parser); + /* * Token fetchers. */ @@ -1214,7 +1217,7 @@ yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column, if (parser->flow_level) return 1; - if (parser->indent < column) + if (parser->indent == -1 || parser->indent < column) { /* * Push the current indentation level to the stack and set the new @@ -1266,7 +1269,16 @@ yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column) if (parser->flow_level) return 1; - /* Loop through the indentation levels in the stack. */ + /* + * column is unsigned and parser->indent is signed, so if + * parser->indent is less than zero the conditional in the while + * loop below is incorrect. Guard against that. + */ + + if (parser->indent < 0) + return 1; + + /* Loop through the intendation levels in the stack. */ while (parser->indent > column) { @@ -1285,6 +1297,41 @@ yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column) return 1; } +/* + * Pop indentation levels from the indents stack until the current + * level resets to -1. For each intendation level, append the + * BLOCK-END token. + */ + +static int +yaml_parser_reset_indent(yaml_parser_t *parser) +{ + yaml_token_t token; + + /* In the flow context, do nothing. */ + + if (parser->flow_level) + return 1; + + /* Loop through the intendation levels in the stack. */ + + while (parser->indent > -1) + { + /* Create a token and append it to the queue. */ + + TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark); + + if (!ENQUEUE(parser, parser->tokens, token)) + return 0; + + /* Pop the indentation level. */ + + parser->indent = POP(parser, parser->indents); + } + + return 1; +} + /* * Initialize the scanner and produce the STREAM-START token. */ @@ -1341,7 +1388,7 @@ yaml_parser_fetch_stream_end(yaml_parser_t *parser) /* Reset the indentation level. */ - if (!yaml_parser_unroll_indent(parser, -1)) + if (!yaml_parser_reset_indent(parser)) return 0; /* Reset simple keys. */ @@ -1372,7 +1419,7 @@ yaml_parser_fetch_directive(yaml_parser_t *parser) /* Reset the indentation level. */ - if (!yaml_parser_unroll_indent(parser, -1)) + if (!yaml_parser_reset_indent(parser)) return 0; /* Reset simple keys. */ @@ -1410,7 +1457,7 @@ yaml_parser_fetch_document_indicator(yaml_parser_t *parser, /* Reset the indentation level. */ - if (!yaml_parser_unroll_indent(parser, -1)) + if (!yaml_parser_reset_indent(parser)) return 0; /* Reset simple keys. */ From 66854ec368b89934b03576e421e2c12ed9e44377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingy=20d=C3=B6t=20Net?= Date: Fri, 28 Nov 2014 08:37:00 -0800 Subject: [PATCH 03/21] Sync to head of https://bitbucket.org/xi/libyaml --- src/api.c | 7 +----- src/scanner.c | 59 ++++++++++----------------------------------------- 2 files changed, 12 insertions(+), 54 deletions(-) diff --git a/src/api.c b/src/api.c index 35a58c5b..b1a8da0b 100644 --- a/src/api.c +++ b/src/api.c @@ -117,12 +117,7 @@ yaml_string_join( YAML_DECLARE(int) yaml_stack_extend(void **start, void **top, void **end) { - void *new_start; - - if ((char *)*end - (char *)*start >= INT_MAX / 2) - return 0; - - new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2); + void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2); if (!new_start) return 0; diff --git a/src/scanner.c b/src/scanner.c index b74b961b..53993a65 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -1108,6 +1108,13 @@ yaml_parser_save_simple_key(yaml_parser_t *parser) int required = (!parser->flow_level && parser->indent == (ptrdiff_t)parser->mark.column); + /* + * A simple key is required only when it is the first token in the current + * line. Therefore it is always allowed. But we add a check anyway. + */ + + assert(parser->simple_key_allowed || !required); /* Impossible. */ + /* * If the current position may start a simple key, save it. */ @@ -1217,7 +1224,7 @@ yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column, if (parser->flow_level) return 1; - if (parser->indent == -1 || parser->indent < column) + if (parser->indent < column) { /* * Push the current indentation level to the stack and set the new @@ -1269,15 +1276,6 @@ yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column) if (parser->flow_level) return 1; - /* - * column is unsigned and parser->indent is signed, so if - * parser->indent is less than zero the conditional in the while - * loop below is incorrect. Guard against that. - */ - - if (parser->indent < 0) - return 1; - /* Loop through the intendation levels in the stack. */ while (parser->indent > column) @@ -1297,41 +1295,6 @@ yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column) return 1; } -/* - * Pop indentation levels from the indents stack until the current - * level resets to -1. For each intendation level, append the - * BLOCK-END token. - */ - -static int -yaml_parser_reset_indent(yaml_parser_t *parser) -{ - yaml_token_t token; - - /* In the flow context, do nothing. */ - - if (parser->flow_level) - return 1; - - /* Loop through the intendation levels in the stack. */ - - while (parser->indent > -1) - { - /* Create a token and append it to the queue. */ - - TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark); - - if (!ENQUEUE(parser, parser->tokens, token)) - return 0; - - /* Pop the indentation level. */ - - parser->indent = POP(parser, parser->indents); - } - - return 1; -} - /* * Initialize the scanner and produce the STREAM-START token. */ @@ -1388,7 +1351,7 @@ yaml_parser_fetch_stream_end(yaml_parser_t *parser) /* Reset the indentation level. */ - if (!yaml_parser_reset_indent(parser)) + if (!yaml_parser_unroll_indent(parser, -1)) return 0; /* Reset simple keys. */ @@ -1419,7 +1382,7 @@ yaml_parser_fetch_directive(yaml_parser_t *parser) /* Reset the indentation level. */ - if (!yaml_parser_reset_indent(parser)) + if (!yaml_parser_unroll_indent(parser, -1)) return 0; /* Reset simple keys. */ @@ -1457,7 +1420,7 @@ yaml_parser_fetch_document_indicator(yaml_parser_t *parser, /* Reset the indentation level. */ - if (!yaml_parser_reset_indent(parser)) + if (!yaml_parser_unroll_indent(parser, -1)) return 0; /* Reset simple keys. */ From 9a626d77b78d847101e6b8a2b5a2d31fdb6fc01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingy=20d=C3=B6t=20Net?= Date: Fri, 28 Nov 2014 09:21:49 -0800 Subject: [PATCH 04/21] Fix for https://bitbucket.org/xi/libyaml/issue/10/ https://bitbucket.org/xi/libyaml/issue/10/wrapped-strings-cause-assert-failure Commenting out the assert makes the scanner do the right thing and results in just a simple parse failure. --- src/scanner.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/scanner.c b/src/scanner.c index 53993a65..cee0ba50 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -1113,7 +1113,9 @@ yaml_parser_save_simple_key(yaml_parser_t *parser) * line. Therefore it is always allowed. But we add a check anyway. */ - assert(parser->simple_key_allowed || !required); /* Impossible. */ + /* XXX This caused: + * https://bitbucket.org/xi/libyaml/issue/10/wrapped-strings-cause-assert-failure + assert(parser->simple_key_allowed || !required); */ /* Impossible. */ /* * If the current position may start a simple key, save it. From b144bb5abbddce1df46006bfacc3d9e0eae9b702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingy=20d=C3=B6t=20Net?= Date: Mon, 11 Feb 2013 11:16:08 -0800 Subject: [PATCH 05/21] Add a development directory. This directory contains instructions and code for getting a libyaml development environment running. --- development/README | 36 +++++++++++++++++++++++++++ development/ToDo | 2 ++ development/clean | 12 +++++++++ development/setup.cfg | 5 ++++ development/test | 58 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 development/README create mode 100644 development/ToDo create mode 100755 development/clean create mode 100644 development/setup.cfg create mode 100755 development/test diff --git a/development/README b/development/README new file mode 100644 index 00000000..a33e1426 --- /dev/null +++ b/development/README @@ -0,0 +1,36 @@ += libyaml Development + +Welcome! + +The directory contains extra content for development of libyaml. It should only +exist on branches and should not be merged to master. + +All libyaml development should be done on branches. GitHub commit/push will be +granted liberally. Stop by #yaml on irc.freenode.net. + +== Quick start + + $ git clone https://github.com/yaml/libyaml.git + $ cd libyaml + $ ./developent/test + +== Testing + +Testing libyaml requires the pyyaml repository. The tests are all in the +pyyamlrepository. + +The following script will make sure that everything is set up correctly and run +the tests for you: + + $ ./developent/test + +== PyYAML + +Note that changes to libyaml should be replicated in ../pyyaml/lib and +../pyyaml/lib3 for Python and Python3 respectively. + +== NOTE + +There is an unsolved problem where the build system will pick up a system +libyaml instead of the development one. For now make sure there is no system +libyaml. Patches (very much) welcome. diff --git a/development/ToDo b/development/ToDo new file mode 100644 index 00000000..a555fde0 --- /dev/null +++ b/development/ToDo @@ -0,0 +1,2 @@ +- When there is a system libyaml available, pyyaml will link to that even + though we tell it to use the local version. I don't understand why. diff --git a/development/clean b/development/clean new file mode 100755 index 00000000..3f40990e --- /dev/null +++ b/development/clean @@ -0,0 +1,12 @@ +#!/bin/bash + +set -ex + +git clean -dxf +( + cd ../pyyaml + git checkout setup.cfg + rm -fr build + rm -f ext/_yaml.c + rm -f tests/lib/*.pyc +) diff --git a/development/setup.cfg b/development/setup.cfg new file mode 100644 index 00000000..f29ef126 --- /dev/null +++ b/development/setup.cfg @@ -0,0 +1,5 @@ +# DO NOT COMMIT +# Test setup.cfg from libyaml for libyaml testing. +[build_ext] +include_dirs=../libyaml/include +library_dirs=../libyaml/src/.libs diff --git a/development/test b/development/test new file mode 100755 index 00000000..c4653709 --- /dev/null +++ b/development/test @@ -0,0 +1,58 @@ +#!/bin/bash + +set -ex + +test_branch=yaml-1.2 +pyyaml=../pyyaml +[ -n "$LIBYAML_REPO_NAME" ] || LIBYAML_REPO_NAME=libyaml + +if [[ $(basename $PWD) != $LIBYAML_REPO_NAME ]]; then + echo "This does not appear to be the libyaml repository" + echo "If it is, set LIBYAML_REPO_NAME" + exit 1 +fi + +if [ ! -d .git ]; then + echo "Run this script from the top level directory of the libyaml repo" + exit 1 +fi + +if [ -z "$(which cython)" ]; then + echo "You need to install cython" + exit 1 +fi + +libyaml_branch=$(git rev-parse --abbrev-ref HEAD) +if [ $libyaml_branch != $test_branch ]; then + echo "git branch must be set to '$test_branch'" + exit 1 +fi + +if [ -e $pyyaml ]; then + if [ ! -d $pyyaml/.git ]; then + echo "$pyyaml must be a Git repository" + exit 1 + fi +else + git clone git://github.com/yaml/pyyaml.git $pyyaml + (cd $pyyaml; git branch $pyyaml) +fi + +pyyaml_branch=$(cd $pyyaml; git rev-parse --abbrev-ref HEAD) +if [ $pyyaml_branch != $test_branch ]; then + echo "$pyyaml must be on branch '$test_branch'" + exit 1 +fi + +[ ! -e ./configure ] && ./bootstrap +[ ! -e ./Makefile ] && ./configure + +make + +cp development/setup.cfg $pyyaml/ +( + cd $pyyaml + rm -fr build + python setup.py build test +) + From 103177787695373145405e07c831be00ce8bfb97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingy=20d=C3=B6t=20Net?= Date: Mon, 22 Dec 2014 17:13:57 -0800 Subject: [PATCH 06/21] Use portable comments --- src/emitter.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/emitter.c b/src/emitter.c index 901f07fa..39b03dfd 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -1946,6 +1946,15 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, emitter->whitespace = 0; emitter->indention = 0; +/* + * < rz> ingy: i'm not sure why i set open_ended in yaml_emitter_write_plain_scalar + * + * Disabling this as it breaks YAML::XS tests with no perceived benefit. + * if (emitter->root_context) + * { + * emitter->open_ended = 1; + * } + */ return 1; } From b71ce3fe716529b17c88224c2f5f9f229de85fa3 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Sat, 27 Feb 2016 11:45:51 +0100 Subject: [PATCH 07/21] Fixed most compiler warnings -Wall -Wextra repro: CFLAGS="-Wall -Wextra -Wunused-parameter -Wc++-compat" ./configure && make which we use for perl, and libyaml is now included in cperl. Tested with gcc-5 and clang-3.7 There are still a tons of format warnings (%d on 64bit) in example-deconstructor.c which I skipped. --- src/api.c | 51 ++++++++++++++++++----------------- src/dumper.c | 4 +-- src/emitter.c | 4 +-- src/loader.c | 8 +++--- src/parser.c | 22 +++++++-------- src/scanner.c | 8 +++--- src/yaml_private.h | 39 ++++++++++++++++++++++----- tests/example-deconstructor.c | 28 +++++++++---------- tests/example-reformatter.c | 28 +++++++++---------- tests/run-dumper.c | 6 ++--- tests/run-emitter.c | 6 ++--- tests/test-reader.c | 28 +++++++++---------- tests/test-version.c | 6 ++--- 13 files changed, 129 insertions(+), 109 deletions(-) diff --git a/src/api.c b/src/api.c index b1a8da0b..ee170d87 100644 --- a/src/api.c +++ b/src/api.c @@ -74,7 +74,7 @@ YAML_DECLARE(int) yaml_string_extend(yaml_char_t **start, yaml_char_t **pointer, yaml_char_t **end) { - yaml_char_t *new_start = yaml_realloc(*start, (*end - *start)*2); + yaml_char_t *new_start = (yaml_char_t *)yaml_realloc((void*)*start, (*end - *start)*2); if (!new_start) return 0; @@ -94,8 +94,9 @@ yaml_string_extend(yaml_char_t **start, YAML_DECLARE(int) yaml_string_join( yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end, - yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end) + yaml_char_t **b_start, yaml_char_t **b_pointer, SHIM(yaml_char_t **b_end)) { + UNUSED_PARAM(b_end) if (*b_start == *b_pointer) return 1; @@ -177,17 +178,17 @@ yaml_parser_initialize(yaml_parser_t *parser) goto error; if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE)) goto error; - if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE)) + if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE, yaml_token_t*)) goto error; - if (!STACK_INIT(parser, parser->indents, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, parser->indents, int*)) goto error; - if (!STACK_INIT(parser, parser->simple_keys, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, parser->simple_keys, yaml_simple_key_t*)) goto error; - if (!STACK_INIT(parser, parser->states, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, parser->states, yaml_parser_state_t*)) goto error; - if (!STACK_INIT(parser, parser->marks, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, parser->marks, yaml_mark_t*)) goto error; - if (!STACK_INIT(parser, parser->tag_directives, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, parser->tag_directives, yaml_tag_directive_t*)) goto error; return 1; @@ -243,7 +244,7 @@ static int yaml_string_read_handler(void *data, unsigned char *buffer, size_t size, size_t *size_read) { - yaml_parser_t *parser = data; + yaml_parser_t *parser = (yaml_parser_t *)data; if (parser->input.string.current == parser->input.string.end) { *size_read = 0; @@ -269,7 +270,7 @@ static int yaml_file_read_handler(void *data, unsigned char *buffer, size_t size, size_t *size_read) { - yaml_parser_t *parser = data; + yaml_parser_t *parser = (yaml_parser_t *)data; *size_read = fread(buffer, 1, size, parser->input.file); return !ferror(parser->input.file); @@ -355,13 +356,13 @@ yaml_emitter_initialize(yaml_emitter_t *emitter) goto error; if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE)) goto error; - if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_SIZE)) + if (!STACK_INIT(emitter, emitter->states, yaml_emitter_state_t*)) goto error; - if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE)) + if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE, yaml_event_t*)) goto error; - if (!STACK_INIT(emitter, emitter->indents, INITIAL_STACK_SIZE)) + if (!STACK_INIT(emitter, emitter->indents, int*)) goto error; - if (!STACK_INIT(emitter, emitter->tag_directives, INITIAL_STACK_SIZE)) + if (!STACK_INIT(emitter, emitter->tag_directives, yaml_tag_directive_t*)) goto error; return 1; @@ -413,7 +414,7 @@ yaml_emitter_delete(yaml_emitter_t *emitter) static int yaml_string_write_handler(void *data, unsigned char *buffer, size_t size) { - yaml_emitter_t *emitter = data; + yaml_emitter_t *emitter = (yaml_emitter_t *)data; if (emitter->output.string.size - *emitter->output.string.size_written < size) { @@ -439,7 +440,7 @@ yaml_string_write_handler(void *data, unsigned char *buffer, size_t size) static int yaml_file_write_handler(void *data, unsigned char *buffer, size_t size) { - yaml_emitter_t *emitter = data; + yaml_emitter_t *emitter = (yaml_emitter_t *)data; return (fwrite(buffer, 1, size, emitter->output.file) == size); } @@ -717,7 +718,7 @@ yaml_document_start_event_initialize(yaml_event_t *event, /* Valid tag directives are expected. */ if (version_directive) { - version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)); + version_directive_copy = YAML_MALLOC_STATIC(yaml_version_directive_t); if (!version_directive_copy) goto error; version_directive_copy->major = version_directive->major; version_directive_copy->minor = version_directive->minor; @@ -725,7 +726,7 @@ yaml_document_start_event_initialize(yaml_event_t *event, if (tag_directives_start != tag_directives_end) { yaml_tag_directive_t *tag_directive; - if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) + if (!STACK_INIT(&context, tag_directives_copy, yaml_tag_directive_t*)) goto error; for (tag_directive = tag_directives_start; tag_directive != tag_directives_end; tag_directive ++) { @@ -843,7 +844,7 @@ yaml_scalar_event_initialize(yaml_event_t *event, } if (!yaml_check_utf8(value, length)) goto error; - value_copy = yaml_malloc(length+1); + value_copy = YAML_MALLOC(length+1); if (!value_copy) goto error; memcpy(value_copy, value, length); value_copy[length] = '\0'; @@ -1055,10 +1056,10 @@ yaml_document_initialize(yaml_document_t *document, (tag_directives_start == tag_directives_end)); /* Valid tag directives are expected. */ - if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error; + if (!STACK_INIT(&context, nodes, yaml_node_t*)) goto error; if (version_directive) { - version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)); + version_directive_copy = YAML_MALLOC_STATIC(yaml_version_directive_t); if (!version_directive_copy) goto error; version_directive_copy->major = version_directive->major; version_directive_copy->minor = version_directive->minor; @@ -1066,7 +1067,7 @@ yaml_document_initialize(yaml_document_t *document, if (tag_directives_start != tag_directives_end) { yaml_tag_directive_t *tag_directive; - if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) + if (!STACK_INIT(&context, tag_directives_copy, yaml_tag_directive_t*)) goto error; for (tag_directive = tag_directives_start; tag_directive != tag_directives_end; tag_directive ++) { @@ -1219,7 +1220,7 @@ yaml_document_add_scalar(yaml_document_t *document, } if (!yaml_check_utf8(value, length)) goto error; - value_copy = yaml_malloc(length+1); + value_copy = YAML_MALLOC(length+1); if (!value_copy) goto error; memcpy(value_copy, value, length); value_copy[length] = '\0'; @@ -1266,7 +1267,7 @@ yaml_document_add_sequence(yaml_document_t *document, tag_copy = yaml_strdup(tag); if (!tag_copy) goto error; - if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error; + if (!STACK_INIT(&context, items, yaml_node_item_t*)) goto error; SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end, style, mark, mark); @@ -1311,7 +1312,7 @@ yaml_document_add_mapping(yaml_document_t *document, tag_copy = yaml_strdup(tag); if (!tag_copy) goto error; - if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error; + if (!STACK_INIT(&context, pairs, yaml_node_pair_t*)) goto error; MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, style, mark, mark); diff --git a/src/dumper.c b/src/dumper.c index 203c6a70..29fb9c07 100644 --- a/src/dumper.c +++ b/src/dumper.c @@ -245,9 +245,9 @@ yaml_emitter_anchor_node(yaml_emitter_t *emitter, int index) #define ANCHOR_TEMPLATE_LENGTH 16 static yaml_char_t * -yaml_emitter_generate_anchor(yaml_emitter_t *emitter, int anchor_id) +yaml_emitter_generate_anchor(SHIM(yaml_emitter_t *emitter), int anchor_id) { - yaml_char_t *anchor = yaml_malloc(ANCHOR_TEMPLATE_LENGTH); + yaml_char_t *anchor = YAML_MALLOC(ANCHOR_TEMPLATE_LENGTH); if (!anchor) return NULL; diff --git a/src/emitter.c b/src/emitter.c index 39b03dfd..4e9e0315 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -1002,7 +1002,7 @@ yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, */ static int -yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event) +yaml_emitter_emit_alias(yaml_emitter_t *emitter, SHIM(yaml_event_t *event)) { if (!yaml_emitter_process_anchor(emitter)) return 0; @@ -1087,7 +1087,7 @@ yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event) */ static int -yaml_emitter_check_empty_document(yaml_emitter_t *emitter) +yaml_emitter_check_empty_document(SHIM(yaml_emitter_t *emitter)) { return 0; } diff --git a/src/loader.c b/src/loader.c index 3ba99f08..db8501ac 100644 --- a/src/loader.c +++ b/src/loader.c @@ -72,7 +72,7 @@ yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document) assert(document); /* Non-NULL document object is expected. */ memset(document, 0, sizeof(yaml_document_t)); - if (!STACK_INIT(parser, document->nodes, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, document->nodes, yaml_node_t*)) goto error; if (!parser->stream_start_produced) { @@ -90,7 +90,7 @@ yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document) return 1; } - if (!STACK_INIT(parser, parser->aliases, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, parser->aliases, yaml_alias_data_t*)) goto error; parser->document = document; @@ -339,7 +339,7 @@ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event) if (!tag) goto error; } - if (!STACK_INIT(parser, items, INITIAL_STACK_SIZE)) goto error; + if (!STACK_INIT(parser, items, yaml_node_item_t*)) goto error; SEQUENCE_NODE_INIT(node, tag, items.start, items.end, first_event->data.sequence_start.style, @@ -402,7 +402,7 @@ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event) if (!tag) goto error; } - if (!STACK_INIT(parser, pairs, INITIAL_STACK_SIZE)) goto error; + if (!STACK_INIT(parser, pairs, yaml_node_pair_t*)) goto error; MAPPING_NODE_INIT(node, tag, pairs.start, pairs.end, first_event->data.mapping_start.style, diff --git a/src/parser.c b/src/parser.c index dc5430b0..621f676b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -605,7 +605,7 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, if (strcmp((char *)tag_directive->handle, (char *)tag_handle) == 0) { size_t prefix_len = strlen((char *)tag_directive->prefix); size_t suffix_len = strlen((char *)tag_suffix); - tag = yaml_malloc(prefix_len+suffix_len+1); + tag = YAML_MALLOC(prefix_len+suffix_len+1); if (!tag) { parser->error = YAML_MEMORY_ERROR; goto error; @@ -685,7 +685,7 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, return 1; } else if (anchor || tag) { - yaml_char_t *value = yaml_malloc(1); + yaml_char_t *value = YAML_MALLOC(1); if (!value) { parser->error = YAML_MEMORY_ERROR; goto error; @@ -759,9 +759,8 @@ yaml_parser_parse_block_sequence_entry(yaml_parser_t *parser, else if (token->type == YAML_BLOCK_END_TOKEN) { - yaml_mark_t dummy_mark; /* Used to eliminate a compiler warning. */ parser->state = POP(parser, parser->states); - dummy_mark = POP(parser, parser->marks); + (void)POP(parser, parser->marks); SEQUENCE_END_EVENT_INIT(*event, token->start_mark, token->end_mark); SKIP_TOKEN(parser); return 1; @@ -869,9 +868,8 @@ yaml_parser_parse_block_mapping_key(yaml_parser_t *parser, else if (token->type == YAML_BLOCK_END_TOKEN) { - yaml_mark_t dummy_mark; /* Used to eliminate a compiler warning. */ parser->state = POP(parser, parser->states); - dummy_mark = POP(parser, parser->marks); + (void)POP(parser, parser->marks); MAPPING_END_EVENT_INIT(*event, token->start_mark, token->end_mark); SKIP_TOKEN(parser); return 1; @@ -952,7 +950,6 @@ yaml_parser_parse_flow_sequence_entry(yaml_parser_t *parser, yaml_event_t *event, int first) { yaml_token_t *token; - yaml_mark_t dummy_mark; /* Used to eliminate a compiler warning. */ if (first) { token = PEEK_TOKEN(parser); @@ -997,7 +994,7 @@ yaml_parser_parse_flow_sequence_entry(yaml_parser_t *parser, } parser->state = POP(parser, parser->states); - dummy_mark = POP(parser, parser->marks); + (void)POP(parser, parser->marks); SEQUENCE_END_EVENT_INIT(*event, token->start_mark, token->end_mark); SKIP_TOKEN(parser); return 1; @@ -1104,7 +1101,6 @@ yaml_parser_parse_flow_mapping_key(yaml_parser_t *parser, yaml_event_t *event, int first) { yaml_token_t *token; - yaml_mark_t dummy_mark; /* Used to eliminate a compiler warning. */ if (first) { token = PEEK_TOKEN(parser); @@ -1158,7 +1154,7 @@ yaml_parser_parse_flow_mapping_key(yaml_parser_t *parser, } parser->state = POP(parser, parser->states); - dummy_mark = POP(parser, parser->marks); + (void)POP(parser, parser->marks); MAPPING_END_EVENT_INIT(*event, token->start_mark, token->end_mark); SKIP_TOKEN(parser); return 1; @@ -1212,7 +1208,7 @@ yaml_parser_process_empty_scalar(yaml_parser_t *parser, yaml_event_t *event, { yaml_char_t *value; - value = yaml_malloc(1); + value = YAML_MALLOC(1); if (!value) { parser->error = YAML_MEMORY_ERROR; return 0; @@ -1249,7 +1245,7 @@ yaml_parser_process_directives(yaml_parser_t *parser, } tag_directives = { NULL, NULL, NULL }; yaml_token_t *token; - if (!STACK_INIT(parser, tag_directives, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, tag_directives, yaml_tag_directive_t*)) goto error; token = PEEK_TOKEN(parser); @@ -1270,7 +1266,7 @@ yaml_parser_process_directives(yaml_parser_t *parser, "found incompatible YAML document", token->start_mark); goto error; } - version_directive = yaml_malloc(sizeof(yaml_version_directive_t)); + version_directive = YAML_MALLOC_STATIC(yaml_version_directive_t); if (!version_directive) { parser->error = YAML_MEMORY_ERROR; goto error; diff --git a/src/scanner.c b/src/scanner.c index cee0ba50..4ae56390 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -1198,11 +1198,9 @@ yaml_parser_increase_flow_level(yaml_parser_t *parser) static int yaml_parser_decrease_flow_level(yaml_parser_t *parser) { - yaml_simple_key_t dummy_key; /* Used to eliminate a compiler warning. */ - if (parser->flow_level) { parser->flow_level --; - dummy_key = POP(parser, parser->simple_keys); + (void)POP(parser, parser->simple_keys); } return 1; @@ -2413,7 +2411,7 @@ yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token) { /* Set the handle to '' */ - handle = yaml_malloc(1); + handle = YAML_MALLOC(1); if (!handle) goto error; handle[0] = '\0'; @@ -2465,7 +2463,7 @@ yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token) /* Set the handle to '!'. */ yaml_free(handle); - handle = yaml_malloc(2); + handle = YAML_MALLOC(2); if (!handle) goto error; handle[0] = '!'; handle[1] = '\0'; diff --git a/src/yaml_private.h b/src/yaml_private.h index faa28ead..bc45f15e 100644 --- a/src/yaml_private.h +++ b/src/yaml_private.h @@ -89,7 +89,7 @@ yaml_parser_fetch_more_tokens(yaml_parser_t *parser); */ #define BUFFER_INIT(context,buffer,size) \ - (((buffer).start = yaml_malloc(size)) ? \ + (((buffer).start = (yaml_char_t *)yaml_malloc(size)) ? \ ((buffer).last = (buffer).pointer = (buffer).start, \ (buffer).end = (buffer).start+(size), \ 1) : \ @@ -129,7 +129,7 @@ yaml_string_join( (value).pointer = (string)) #define STRING_INIT(context,string,size) \ - (((string).start = yaml_malloc(size)) ? \ + (((string).start = YAML_MALLOC(size)) ? \ ((string).pointer = (string).start, \ (string).end = (string).start+(size), \ memset((string).start, 0, (size)), \ @@ -419,10 +419,10 @@ yaml_stack_extend(void **start, void **top, void **end); YAML_DECLARE(int) yaml_queue_extend(void **start, void **head, void **tail, void **end); -#define STACK_INIT(context,stack,size) \ - (((stack).start = yaml_malloc((size)*sizeof(*(stack).start))) ? \ +#define STACK_INIT(context,stack,type) \ + (((stack).start = (type)yaml_malloc(INITIAL_STACK_SIZE*sizeof(*(stack).start))) ? \ ((stack).top = (stack).start, \ - (stack).end = (stack).start+(size), \ + (stack).end = (stack).start+INITIAL_STACK_SIZE, \ 1) : \ ((context)->error = YAML_MEMORY_ERROR, \ 0)) @@ -452,8 +452,8 @@ yaml_queue_extend(void **start, void **head, void **tail, void **end); #define POP(context,stack) \ (*(--(stack).top)) -#define QUEUE_INIT(context,queue,size) \ - (((queue).start = yaml_malloc((size)*sizeof(*(queue).start))) ? \ +#define QUEUE_INIT(context,queue,size,type) \ + (((queue).start = (type)yaml_malloc((size)*sizeof(*(queue).start))) ? \ ((queue).head = (queue).tail = (queue).start, \ (queue).end = (queue).start+(size), \ 1) : \ @@ -656,3 +656,28 @@ yaml_queue_extend(void **start, void **head, void **tail, void **end); (node).data.mapping.pairs.end = (node_pairs_end), \ (node).data.mapping.pairs.top = (node_pairs_start), \ (node).data.mapping.style = (node_style)) + +/* Strict C compiler warning helpers */ + +#if defined(__clang__) || defined(__GNUC__) +# define HASATTRIBUTE_UNUSED +#endif +#ifdef HASATTRIBUTE_UNUSED +# define __attribute__unused__ __attribute__((__unused__)) +#else +# define __attribute__unused__ +#endif + +/* Shim arguments are arguments that must be included in your function, + * but serve no purpose inside. Silence compiler warnings. */ +#define SHIM(a) /*@unused@*/ a __attribute__unused__ + +/* UNUSED_PARAM() marks a shim argument in the body to silence compiler warnings */ +#ifdef __clang__ +# define UNUSED_PARAM(a) (void)(a); +#else +# define UNUSED_PARAM(a) /*@-noeffect*/if (0) (void)(a)/*@=noeffect*/; +#endif + +#define YAML_MALLOC_STATIC(type) (type*)yaml_malloc(sizeof(type)) +#define YAML_MALLOC(size) (yaml_char_t *)yaml_malloc(size) diff --git a/tests/example-deconstructor.c b/tests/example-deconstructor.c index 3ad99c10..0a768057 100644 --- a/tests/example-deconstructor.c +++ b/tests/example-deconstructor.c @@ -1033,12 +1033,12 @@ main(int argc, char *argv[]) case YAML_READER_ERROR: if (parser.problem_value != -1) { - fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem, - parser.problem_value, parser.problem_offset); + fprintf(stderr, "Reader error: %s: #%X at %ld\n", parser.problem, + parser.problem_value, (long)parser.problem_offset); } else { - fprintf(stderr, "Reader error: %s at %d\n", parser.problem, - parser.problem_offset); + fprintf(stderr, "Reader error: %s at %ld\n", parser.problem, + (long)parser.problem_offset); } break; @@ -1046,14 +1046,14 @@ main(int argc, char *argv[]) if (parser.context) { fprintf(stderr, "Scanner error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, - parser.context_mark.line+1, parser.context_mark.column+1, - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + (int)parser.context_mark.line+1, (int)parser.context_mark.column+1, + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } else { fprintf(stderr, "Scanner error: %s at line %d, column %d\n", - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } break; @@ -1061,14 +1061,14 @@ main(int argc, char *argv[]) if (parser.context) { fprintf(stderr, "Parser error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, - parser.context_mark.line+1, parser.context_mark.column+1, - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + (int)parser.context_mark.line+1, (int)parser.context_mark.column+1, + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } else { fprintf(stderr, "Parser error: %s at line %d, column %d\n", - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } break; diff --git a/tests/example-reformatter.c b/tests/example-reformatter.c index 946d5561..08f860c4 100644 --- a/tests/example-reformatter.c +++ b/tests/example-reformatter.c @@ -120,12 +120,12 @@ main(int argc, char *argv[]) case YAML_READER_ERROR: if (parser.problem_value != -1) { - fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem, - parser.problem_value, parser.problem_offset); + fprintf(stderr, "Reader error: %s: #%X at %ld\n", parser.problem, + parser.problem_value, (long)parser.problem_offset); } else { - fprintf(stderr, "Reader error: %s at %d\n", parser.problem, - parser.problem_offset); + fprintf(stderr, "Reader error: %s at %ld\n", parser.problem, + (long)parser.problem_offset); } break; @@ -133,14 +133,14 @@ main(int argc, char *argv[]) if (parser.context) { fprintf(stderr, "Scanner error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, - parser.context_mark.line+1, parser.context_mark.column+1, - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + (int)parser.context_mark.line+1, (int)parser.context_mark.column+1, + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } else { fprintf(stderr, "Scanner error: %s at line %d, column %d\n", - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } break; @@ -148,14 +148,14 @@ main(int argc, char *argv[]) if (parser.context) { fprintf(stderr, "Parser error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, - parser.context_mark.line+1, parser.context_mark.column+1, - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + (int)parser.context_mark.line+1, (int)parser.context_mark.column+1, + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } else { fprintf(stderr, "Parser error: %s at line %d, column %d\n", - parser.problem, parser.problem_mark.line+1, - parser.problem_mark.column+1); + parser.problem, (int)parser.problem_mark.line+1, + (int)parser.problem_mark.column+1); } break; diff --git a/tests/run-dumper.c b/tests/run-dumper.c index 390d982a..302b9b96 100644 --- a/tests/run-dumper.c +++ b/tests/run-dumper.c @@ -180,8 +180,8 @@ int print_output(char *name, unsigned char *buffer, size_t size, int count) if (feof(file)) break; } fclose(file); - printf("#### (length: %d)\n", total_size); - printf("OUTPUT:\n%s#### (length: %d)\n", buffer, size); + printf("#### (length: %ld)\n", (long)total_size); + printf("OUTPUT:\n%s#### (length: %ld)\n", buffer, (long)size); return 0; } @@ -304,7 +304,7 @@ main(int argc, char *argv[]) yaml_document_delete(documents+k); } - printf("PASSED (length: %d)\n", written); + printf("PASSED (length: %ld)\n", (long)written); print_output(argv[number], buffer, written, -1); } diff --git a/tests/run-emitter.c b/tests/run-emitter.c index 73ae9a5f..4996a855 100644 --- a/tests/run-emitter.c +++ b/tests/run-emitter.c @@ -205,8 +205,8 @@ int print_output(char *name, unsigned char *buffer, size_t size, int count) if (feof(file)) break; } fclose(file); - printf("#### (length: %d)\n", total_size); - printf("OUTPUT:\n%s#### (length: %d)\n", buffer, size); + printf("#### (length: %ld)\n", (long)total_size); + printf("OUTPUT:\n%s#### (length: %ld)\n", buffer, (long)size); return 0; } @@ -319,7 +319,7 @@ main(int argc, char *argv[]) yaml_event_delete(events+k); } - printf("PASSED (length: %d)\n", written); + printf("PASSED (length: %ld)\n", (long)written); print_output(argv[number], buffer, written, -1); } diff --git a/tests/test-reader.c b/tests/test-reader.c index c6f84cd0..bcd9081a 100644 --- a/tests/test-reader.c +++ b/tests/test-reader.c @@ -144,12 +144,12 @@ int check_utf8_sequences(void) } else if (parser.error == YAML_READER_ERROR) { if (parser.problem_value != -1) { - printf("(reader error: %s: #%X at %d)\n", - parser.problem, parser.problem_value, parser.problem_offset); + printf("(reader error: %s: #%X at %ld)\n", + parser.problem, parser.problem_value, (long)parser.problem_offset); } else { - printf("(reader error: %s at %d)\n", - parser.problem, parser.problem_offset); + printf("(reader error: %s at %ld)\n", + parser.problem, (long)parser.problem_offset); } } if (*end == '!') break; @@ -180,12 +180,12 @@ int check_boms(void) yaml_parser_set_input_string(&parser, (unsigned char *)start, end-start); result = yaml_parser_update_buffer(&parser, end-start); if (!result) { - printf("- (reader error: %s at %d)\n", parser.problem, parser.problem_offset); + printf("- (reader error: %s at %ld)\n", parser.problem, (long)parser.problem_offset); failed++; } else { if (parser.unread != check) { - printf("- (length=%d while expected length=%d)\n", parser.unread, check); + printf("- (length=%ld while expected length=%d)\n", (long)parser.unread, check); failed++; } else if (memcmp(parser.buffer.start, bom_original, check) != 0) { @@ -211,7 +211,7 @@ int check_long_utf8(void) int j; int failed = 0; unsigned char ch0, ch1; - unsigned char *buffer = malloc(3+LONG*2); + unsigned char *buffer = (unsigned char *)malloc(3+LONG*2); assert(buffer); printf("checking a long utf8 sequence...\n"); buffer[k++] = '\xef'; @@ -232,7 +232,7 @@ int check_long_utf8(void) for (k = 0; k < LONG; k++) { if (!parser.unread) { if (!yaml_parser_update_buffer(&parser, 1)) { - printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); + printf("\treader error: %s at %ld\n", parser.problem, (long)parser.problem_offset); failed = 1; break; } @@ -262,11 +262,11 @@ int check_long_utf8(void) } if (!failed) { if (!yaml_parser_update_buffer(&parser, 1)) { - printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); + printf("\treader error: %s at %ld\n", parser.problem, (long)parser.problem_offset); failed = 1; } else if (parser.buffer.pointer[0] != '\0') { - printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser.buffer.pointer[0], parser.eof, parser.unread); + printf("\texpected NUL, found %X (eof=%d, unread=%ld)\n", (int)parser.buffer.pointer[0], parser.eof, (long)parser.unread); failed = 1; } } @@ -283,7 +283,7 @@ int check_long_utf16(void) int j; int failed = 0; unsigned char ch0, ch1; - unsigned char *buffer = malloc(2+LONG*2); + unsigned char *buffer = (unsigned char *)malloc(2+LONG*2); assert(buffer); printf("checking a long utf16 sequence...\n"); buffer[k++] = '\xff'; @@ -303,7 +303,7 @@ int check_long_utf16(void) for (k = 0; k < LONG; k++) { if (!parser.unread) { if (!yaml_parser_update_buffer(&parser, 1)) { - printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); + printf("\treader error: %s at %ld\n", parser.problem, (long)parser.problem_offset); failed = 1; break; } @@ -333,11 +333,11 @@ int check_long_utf16(void) } if (!failed) { if (!yaml_parser_update_buffer(&parser, 1)) { - printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); + printf("\treader error: %s at %ld\n", parser.problem, (long)parser.problem_offset); failed = 1; } else if (parser.buffer.pointer[0] != '\0') { - printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser.buffer.pointer[0], parser.eof, parser.unread); + printf("\texpected NUL, found %X (eof=%d, unread=%ld)\n", (int)parser.buffer.pointer[0], parser.eof, (long)parser.unread); failed = 1; } } diff --git a/tests/test-version.c b/tests/test-version.c index e3e4a162..0c598377 100644 --- a/tests/test-version.c +++ b/tests/test-version.c @@ -21,9 +21,9 @@ main(void) assert(strcmp(buf, yaml_get_version_string()) == 0); /* Print structure sizes. */ - printf("sizeof(token) = %d\n", sizeof(yaml_token_t)); - printf("sizeof(event) = %d\n", sizeof(yaml_event_t)); - printf("sizeof(parser) = %d\n", sizeof(yaml_parser_t)); + printf("sizeof(token) = %ld\n", (long)sizeof(yaml_token_t)); + printf("sizeof(event) = %ld\n", (long)sizeof(yaml_event_t)); + printf("sizeof(parser) = %ld\n", (long)sizeof(yaml_parser_t)); return 0; } From 89a5c4ef99e449beb086dcb5bbb2523c5f9affa9 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Mon, 7 Mar 2016 13:46:30 +0100 Subject: [PATCH 08/21] Bump to 0.1.7 (wip: tests missing) add 2 new flags: * problem_nonstrict to yaml_parser_t * indentless_map to yaml_emitter_t indentless_map is for backcompat with yaml readers. It changes the default indent behavior of map elements, advance the indent the children of the map according to the spec: 6.1 Indentation Spaces: "Each node must be indented further than its parent node. All sibling nodes must use the exact same indentation level. However the content of each sibling node may be further indented independently." http://yaml.org/spec/1.2/spec.html#id2777534 This make it readable by YAML.pm (perl), and behaves the same as all other YAML writers. problem_nonstrict is to skip aborting on some reader errors: "control characters are not allowed" The error is set, but parsing is continued. This allows optionally accepting invalid string values, copy-as-is, avoiding data-loss. --- include/yaml.h | 6 ++++++ src/emitter.c | 2 +- src/reader.c | 11 +++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/yaml.h b/include/yaml.h index 5a04d36d..e1b940ba 100644 --- a/include/yaml.h +++ b/include/yaml.h @@ -1296,6 +1296,10 @@ typedef struct yaml_parser_s { /** The currently parsed document. */ yaml_document_t *document; + /** Optionally forgive some reader errors. + * Make them non-fatal. */ + int problem_nonstrict; + /** * @} */ @@ -1682,6 +1686,8 @@ typedef struct yaml_emitter_s { int indention; /** If an explicit document end is required? */ int open_ended; + /** If a map requires a new indent (backcompat, unreadable by YAML.pm) */ + int indentless_map; /** Anchor analysis. */ struct { diff --git a/src/emitter.c b/src/emitter.c index 4e9e0315..75257bcb 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -869,7 +869,7 @@ yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, if (first) { if (!yaml_emitter_increase_indent(emitter, 0, - (emitter->mapping_context && !emitter->indention))) + emitter->indentless_map && (emitter->mapping_context && !emitter->indention))) return 0; } diff --git a/src/reader.c b/src/reader.c index f1a06deb..1691d10f 100644 --- a/src/reader.c +++ b/src/reader.c @@ -412,10 +412,13 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) || (value >= 0x20 && value <= 0x7E) || (value == 0x85) || (value >= 0xA0 && value <= 0xD7FF) || (value >= 0xE000 && value <= 0xFFFD) - || (value >= 0x10000 && value <= 0x10FFFF))) - return yaml_parser_set_reader_error(parser, - "control characters are not allowed", - parser->offset, value); + || (value >= 0x10000 && value <= 0x10FFFF))) { + int err = yaml_parser_set_reader_error(parser, + "control characters are not allowed", + parser->offset, value); + if (!parser->problem_nonstrict) + return err; + } /* Move the raw pointers. */ From 99b5cb3d14fff57ede06b925f7cd6eb69d8aed89 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Mon, 7 Mar 2016 13:57:05 +0100 Subject: [PATCH 09/21] .travis: make test => check --- .travis.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 402f6317..802f01e7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,4 @@ compiler: - clang - gcc -script: -- ./bootstrap -- ./configure -- make test +script: ./bootstrap && ./configure && make && make check From 3dc95d0d46aa5674077693ea482d43d163e66f11 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Wed, 9 Mar 2016 11:23:45 +0100 Subject: [PATCH 10/21] reformat: return and goto in seperate lines to be able to set breakpoints. also fix some pedantic warnings, remove unused code. --- src/emitter.c | 247 +++++++++++++++++++++++++++++--------------------- src/loader.c | 68 +++++++++----- src/parser.c | 26 ++++-- src/reader.c | 24 ++--- src/scanner.c | 4 +- 5 files changed, 221 insertions(+), 148 deletions(-) diff --git a/src/emitter.c b/src/emitter.c index 75257bcb..5ea3f673 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -993,8 +993,6 @@ yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, return yaml_emitter_set_emitter_error(emitter, "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS"); } - - return 0; } /* @@ -1753,7 +1751,8 @@ yaml_emitter_analyze_event(yaml_emitter_t *emitter, static int yaml_emitter_write_bom(yaml_emitter_t *emitter) { - if (!FLUSH(emitter)) return 0; + if (!FLUSH(emitter)) + return 0; *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF'; *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB'; @@ -1769,11 +1768,13 @@ yaml_emitter_write_indent(yaml_emitter_t *emitter) if (!emitter->indention || emitter->column > indent || (emitter->column == indent && !emitter->whitespace)) { - if (!PUT_BREAK(emitter)) return 0; + if (!PUT_BREAK(emitter)) + return 0; } while (emitter->column < indent) { - if (!PUT(emitter, ' ')) return 0; + if (!PUT(emitter, ' ')) + return 0; } emitter->whitespace = 1; @@ -1794,11 +1795,13 @@ yaml_emitter_write_indicator(yaml_emitter_t *emitter, STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length); if (need_whitespace && !emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; + if (!PUT(emitter, ' ')) + return 0; } while (string.pointer != string.end) { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } emitter->whitespace = is_whitespace; @@ -1816,7 +1819,8 @@ yaml_emitter_write_anchor(yaml_emitter_t *emitter, STRING_ASSIGN(string, value, length); while (string.pointer != string.end) { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } emitter->whitespace = 0; @@ -1833,11 +1837,13 @@ yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, STRING_ASSIGN(string, value, length); if (!emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; + if (!PUT(emitter, ' ')) + return 0; } while (string.pointer != string.end) { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } emitter->whitespace = 0; @@ -1855,7 +1861,8 @@ yaml_emitter_write_tag_content(yaml_emitter_t *emitter, STRING_ASSIGN(string, value, length); if (need_whitespace && !emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; + if (!PUT(emitter, ' ')) + return 0; } while (string.pointer != string.end) { @@ -1870,19 +1877,21 @@ yaml_emitter_write_tag_content(yaml_emitter_t *emitter, || CHECK(string, '\'') || CHECK(string, '(') || CHECK(string, ')') || CHECK(string, '[') || CHECK(string, ']')) { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } else { int width = WIDTH(string); - unsigned int value; + unsigned int v; while (width --) { - value = *(string.pointer++); - if (!PUT(emitter, '%')) return 0; - if (!PUT(emitter, (value >> 4) - + ((value >> 4) < 10 ? '0' : 'A' - 10))) + v = *(string.pointer++); + if (!PUT(emitter, '%')) + return 0; + if (!PUT(emitter, (v >> 4) + + ((v >> 4) < 10 ? '0' : 'A' - 10))) return 0; - if (!PUT(emitter, (value & 0x0F) - + ((value & 0x0F) < 10 ? '0' : 'A' - 10))) + if (!PUT(emitter, (v & 0x0F) + + ((v & 0x0F) < 10 ? '0' : 'A' - 10))) return 0; } } @@ -1905,39 +1914,43 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, STRING_ASSIGN(string, value, length); if (!emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; + if (!PUT(emitter, ' ')) + return 0; } while (string.pointer != string.end) { - if (IS_SPACE(string)) - { + if (IS_SPACE(string)) { if (allow_breaks && !spaces && emitter->column > emitter->best_width && !IS_SPACE_AT(string, 1)) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; MOVE(string); } else { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } spaces = 1; } - else if (IS_BREAK(string)) - { + else if (IS_BREAK(string)) { if (!breaks && CHECK(string, '\n')) { - if (!PUT_BREAK(emitter)) return 0; + if (!PUT_BREAK(emitter)) + return 0; } - if (!WRITE_BREAK(emitter, string)) return 0; + if (!WRITE_BREAK(emitter, string)) + return 0; emitter->indention = 1; breaks = 1; } - else - { + else { if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; } - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; emitter->indention = 0; spaces = 0; breaks = 0; @@ -1974,39 +1987,44 @@ yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, while (string.pointer != string.end) { - if (IS_SPACE(string)) - { + if (IS_SPACE(string)) { if (allow_breaks && !spaces && emitter->column > emitter->best_width && string.pointer != string.start && string.pointer != string.end - 1 && !IS_SPACE_AT(string, 1)) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; MOVE(string); } else { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } spaces = 1; } - else if (IS_BREAK(string)) - { + else if (IS_BREAK(string)) { if (!breaks && CHECK(string, '\n')) { - if (!PUT_BREAK(emitter)) return 0; + if (!PUT_BREAK(emitter)) + return 0; } - if (!WRITE_BREAK(emitter, string)) return 0; + if (!WRITE_BREAK(emitter, string)) + return 0; emitter->indention = 1; breaks = 1; } else { if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; } if (CHECK(string, '\'')) { - if (!PUT(emitter, '\'')) return 0; + if (!PUT(emitter, '\'')) + return 0; } - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; emitter->indention = 0; spaces = 0; breaks = 0; @@ -2042,7 +2060,7 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, { unsigned char octet; unsigned int width; - unsigned int value; + unsigned int v; int k; octet = string.pointer[0]; @@ -2050,121 +2068,142 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, (octet & 0xE0) == 0xC0 ? 2 : (octet & 0xF0) == 0xE0 ? 3 : (octet & 0xF8) == 0xF0 ? 4 : 0; - value = (octet & 0x80) == 0x00 ? octet & 0x7F : + v = (octet & 0x80) == 0x00 ? octet & 0x7F : (octet & 0xE0) == 0xC0 ? octet & 0x1F : (octet & 0xF0) == 0xE0 ? octet & 0x0F : (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; for (k = 1; k < (int)width; k ++) { octet = string.pointer[k]; - value = (value << 6) + (octet & 0x3F); + v = (v << 6) + (octet & 0x3F); } string.pointer += width; - if (!PUT(emitter, '\\')) return 0; + if (!PUT(emitter, '\\')) + return 0; - switch (value) + switch (v) { case 0x00: - if (!PUT(emitter, '0')) return 0; + if (!PUT(emitter, '0')) + return 0; break; case 0x07: - if (!PUT(emitter, 'a')) return 0; + if (!PUT(emitter, 'a')) + return 0; break; case 0x08: - if (!PUT(emitter, 'b')) return 0; + if (!PUT(emitter, 'b')) + return 0; break; case 0x09: - if (!PUT(emitter, 't')) return 0; + if (!PUT(emitter, 't')) + return 0; break; case 0x0A: - if (!PUT(emitter, 'n')) return 0; + if (!PUT(emitter, 'n')) + return 0; break; case 0x0B: - if (!PUT(emitter, 'v')) return 0; + if (!PUT(emitter, 'v')) + return 0; break; case 0x0C: - if (!PUT(emitter, 'f')) return 0; + if (!PUT(emitter, 'f')) + return 0; break; case 0x0D: - if (!PUT(emitter, 'r')) return 0; + if (!PUT(emitter, 'r')) + return 0; break; case 0x1B: - if (!PUT(emitter, 'e')) return 0; + if (!PUT(emitter, 'e')) + return 0; break; case 0x22: - if (!PUT(emitter, '\"')) return 0; + if (!PUT(emitter, '\"')) + return 0; break; case 0x5C: - if (!PUT(emitter, '\\')) return 0; + if (!PUT(emitter, '\\')) + return 0; break; case 0x85: - if (!PUT(emitter, 'N')) return 0; + if (!PUT(emitter, 'N')) + return 0; break; case 0xA0: - if (!PUT(emitter, '_')) return 0; + if (!PUT(emitter, '_')) + return 0; break; case 0x2028: - if (!PUT(emitter, 'L')) return 0; + if (!PUT(emitter, 'L')) + return 0; break; case 0x2029: - if (!PUT(emitter, 'P')) return 0; + if (!PUT(emitter, 'P')) + return 0; break; default: - if (value <= 0xFF) { - if (!PUT(emitter, 'x')) return 0; + if (v <= 0xFF) { + if (!PUT(emitter, 'x')) + return 0; width = 2; } - else if (value <= 0xFFFF) { - if (!PUT(emitter, 'u')) return 0; + else if (v <= 0xFFFF) { + if (!PUT(emitter, 'u')) + return 0; width = 4; } else { - if (!PUT(emitter, 'U')) return 0; + if (!PUT(emitter, 'U')) + return 0; width = 8; } for (k = (width-1)*4; k >= 0; k -= 4) { - int digit = (value >> k) & 0x0F; + int digit = (v >> k) & 0x0F; if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10))) return 0; } } spaces = 0; } - else if (IS_SPACE(string)) - { + else if (IS_SPACE(string)) { if (allow_breaks && !spaces && emitter->column > emitter->best_width && string.pointer != string.start && string.pointer != string.end - 1) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; if (IS_SPACE_AT(string, 1)) { - if (!PUT(emitter, '\\')) return 0; + if (!PUT(emitter, '\\')) + return 0; } MOVE(string); } else { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } spaces = 1; } - else - { - if (!WRITE(emitter, string)) return 0; + else { + if (!WRITE(emitter, string)) + return 0; spaces = 0; } } @@ -2185,8 +2224,7 @@ yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, char indent_hint[2]; char *chomp_hint = NULL; - if (IS_SPACE(string) || IS_BREAK(string)) - { + if (IS_SPACE(string) || IS_BREAK(string)) { indent_hint[0] = '0' + (char)emitter->best_indent; indent_hint[1] = '\0'; if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0)) @@ -2196,39 +2234,32 @@ yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, emitter->open_ended = 0; string.pointer = string.end; - if (string.start == string.pointer) - { + if (string.start == string.pointer) { chomp_hint = "-"; } - else - { + else { do { string.pointer --; } while ((*string.pointer & 0xC0) == 0x80); - if (!IS_BREAK(string)) - { + if (!IS_BREAK(string)) { chomp_hint = "-"; } - else if (string.start == string.pointer) - { + else if (string.start == string.pointer) { chomp_hint = "+"; emitter->open_ended = 1; } - else - { + else { do { string.pointer --; } while ((*string.pointer & 0xC0) == 0x80); - if (IS_BREAK(string)) - { + if (IS_BREAK(string)) { chomp_hint = "+"; emitter->open_ended = 1; } } } - if (chomp_hint) - { + if (chomp_hint) { if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0)) return 0; } @@ -2249,7 +2280,8 @@ yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, return 0; if (!yaml_emitter_write_block_scalar_hints(emitter, string)) return 0; - if (!PUT_BREAK(emitter)) return 0; + if (!PUT_BREAK(emitter)) + return 0; emitter->indention = 1; emitter->whitespace = 1; @@ -2257,16 +2289,19 @@ yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, { if (IS_BREAK(string)) { - if (!WRITE_BREAK(emitter, string)) return 0; + if (!WRITE_BREAK(emitter, string)) + return 0; emitter->indention = 1; breaks = 1; } else { if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; } - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; emitter->indention = 0; breaks = 0; } @@ -2289,7 +2324,8 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, return 0; if (!yaml_emitter_write_block_scalar_hints(emitter, string)) return 0; - if (!PUT_BREAK(emitter)) return 0; + if (!PUT_BREAK(emitter)) + return 0; emitter->indention = 1; emitter->whitespace = 1; @@ -2303,26 +2339,31 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, k += WIDTH_AT(string, k); } if (!IS_BLANKZ_AT(string, k)) { - if (!PUT_BREAK(emitter)) return 0; + if (!PUT_BREAK(emitter)) + return 0; } } - if (!WRITE_BREAK(emitter, string)) return 0; + if (!WRITE_BREAK(emitter, string)) + return 0; emitter->indention = 1; breaks = 1; } else { if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; leading_spaces = IS_BLANK(string); } if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1) && emitter->column > emitter->best_width) { - if (!yaml_emitter_write_indent(emitter)) return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; MOVE(string); } else { - if (!WRITE(emitter, string)) return 0; + if (!WRITE(emitter, string)) + return 0; } emitter->indention = 0; breaks = 0; diff --git a/src/loader.c b/src/loader.c index db8501ac..59e8b404 100644 --- a/src/loader.c +++ b/src/loader.c @@ -76,16 +76,18 @@ yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document) goto error; if (!parser->stream_start_produced) { - if (!yaml_parser_parse(parser, &event)) goto error; + if (!yaml_parser_parse(parser, &event)) + goto error; assert(event.type == YAML_STREAM_START_EVENT); - /* STREAM-START is expected. */ + /* STREAM-START is expected. */ } if (parser->stream_end_produced) { return 1; } - if (!yaml_parser_parse(parser, &event)) goto error; + if (!yaml_parser_parse(parser, &event)) + goto error; if (event.type == YAML_STREAM_END_EVENT) { return 1; } @@ -95,7 +97,8 @@ yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document) parser->document = document; - if (!yaml_parser_load_document(parser, &event)) goto error; + if (!yaml_parser_load_document(parser, &event)) + goto error; yaml_parser_delete_aliases(parser); parser->document = NULL; @@ -286,24 +289,28 @@ yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event) int index; yaml_char_t *tag = first_event->data.scalar.tag; - if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error; + if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) + goto error; if (!tag || strcmp((char *)tag, "!") == 0) { yaml_free(tag); tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SCALAR_TAG); - if (!tag) goto error; + if (!tag) + goto error; } SCALAR_NODE_INIT(node, tag, first_event->data.scalar.value, first_event->data.scalar.length, first_event->data.scalar.style, first_event->start_mark, first_event->end_mark); - if (!PUSH(parser, parser->document->nodes, node)) goto error; + if (!PUSH(parser, parser->document->nodes, node)) + goto error; index = parser->document->nodes.top - parser->document->nodes.start; if (!yaml_parser_register_anchor(parser, index, - first_event->data.scalar.anchor)) return 0; + first_event->data.scalar.anchor)) + return 0; return index; @@ -331,26 +338,31 @@ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event) int index, item_index; yaml_char_t *tag = first_event->data.sequence_start.tag; - if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error; + if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) + goto error; if (!tag || strcmp((char *)tag, "!") == 0) { yaml_free(tag); tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG); - if (!tag) goto error; + if (!tag) + goto error; } - if (!STACK_INIT(parser, items, yaml_node_item_t*)) goto error; + if (!STACK_INIT(parser, items, yaml_node_item_t*)) + goto error; SEQUENCE_NODE_INIT(node, tag, items.start, items.end, first_event->data.sequence_start.style, first_event->start_mark, first_event->end_mark); - if (!PUSH(parser, parser->document->nodes, node)) goto error; + if (!PUSH(parser, parser->document->nodes, node)) + goto error; index = parser->document->nodes.top - parser->document->nodes.start; if (!yaml_parser_register_anchor(parser, index, - first_event->data.sequence_start.anchor)) return 0; + first_event->data.sequence_start.anchor)) + return 0; if (!yaml_parser_parse(parser, &event)) return 0; @@ -394,42 +406,52 @@ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event) yaml_node_pair_t pair; yaml_char_t *tag = first_event->data.mapping_start.tag; - if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error; + if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) + goto error; if (!tag || strcmp((char *)tag, "!") == 0) { yaml_free(tag); tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_MAPPING_TAG); - if (!tag) goto error; + if (!tag) + goto error; } - if (!STACK_INIT(parser, pairs, yaml_node_pair_t*)) goto error; + if (!STACK_INIT(parser, pairs, yaml_node_pair_t*)) + goto error; MAPPING_NODE_INIT(node, tag, pairs.start, pairs.end, first_event->data.mapping_start.style, first_event->start_mark, first_event->end_mark); - if (!PUSH(parser, parser->document->nodes, node)) goto error; + if (!PUSH(parser, parser->document->nodes, node)) + goto error; index = parser->document->nodes.top - parser->document->nodes.start; if (!yaml_parser_register_anchor(parser, index, - first_event->data.mapping_start.anchor)) return 0; + first_event->data.mapping_start.anchor)) + return 0; - if (!yaml_parser_parse(parser, &event)) return 0; + if (!yaml_parser_parse(parser, &event)) + return 0; while (event.type != YAML_MAPPING_END_EVENT) { if (!STACK_LIMIT(parser, parser->document->nodes.start[index-1].data.mapping.pairs, - INT_MAX-1)) return 0; + INT_MAX-1)) + return 0; pair.key = yaml_parser_load_node(parser, &event); if (!pair.key) return 0; - if (!yaml_parser_parse(parser, &event)) return 0; + if (!yaml_parser_parse(parser, &event)) + return 0; pair.value = yaml_parser_load_node(parser, &event); - if (!pair.value) return 0; + if (!pair.value) + return 0; if (!PUSH(parser, parser->document->nodes.start[index-1].data.mapping.pairs, pair)) return 0; - if (!yaml_parser_parse(parser, &event)) return 0; + if (!yaml_parser_parse(parser, &event)) + return 0; } parser->document->nodes.start[index-1].end_mark = event.end_mark; diff --git a/src/parser.c b/src/parser.c index 621f676b..4aaea455 100644 --- a/src/parser.c +++ b/src/parser.c @@ -178,8 +178,9 @@ yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event) /* No events after the end of the stream or error. */ - if (parser->stream_end_produced || parser->error || - parser->state == YAML_PARSE_END_STATE) { + if (parser->stream_end_produced || + parser->error || + parser->state == YAML_PARSE_END_STATE) { return 1; } @@ -390,7 +391,8 @@ yaml_parser_parse_document_start(yaml_parser_t *parser, yaml_event_t *event, &tag_directives.start, &tag_directives.end)) return 0; token = PEEK_TOKEN(parser); - if (!token) goto error; + if (!token) + goto error; if (token->type != YAML_DOCUMENT_START_TOKEN) { yaml_parser_set_parser_error(parser, "did not find expected ", token->start_mark); @@ -560,7 +562,8 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, end_mark = token->end_mark; SKIP_TOKEN(parser); token = PEEK_TOKEN(parser); - if (!token) goto error; + if (!token) + goto error; if (token->type == YAML_TAG_TOKEN) { tag_handle = token->data.tag.handle; @@ -569,7 +572,8 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, end_mark = token->end_mark; SKIP_TOKEN(parser); token = PEEK_TOKEN(parser); - if (!token) goto error; + if (!token) + goto error; } } else if (token->type == YAML_TAG_TOKEN) @@ -580,14 +584,16 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, end_mark = token->end_mark; SKIP_TOKEN(parser); token = PEEK_TOKEN(parser); - if (!token) goto error; + if (!token) + goto error; if (token->type == YAML_ANCHOR_TOKEN) { anchor = token->data.anchor.value; end_mark = token->end_mark; SKIP_TOKEN(parser); token = PEEK_TOKEN(parser); - if (!token) goto error; + if (!token) + goto error; } } @@ -1249,7 +1255,8 @@ yaml_parser_process_directives(yaml_parser_t *parser, goto error; token = PEEK_TOKEN(parser); - if (!token) goto error; + if (!token) + goto error; while (token->type == YAML_VERSION_DIRECTIVE_TOKEN || token->type == YAML_TAG_DIRECTIVE_TOKEN) @@ -1289,7 +1296,8 @@ yaml_parser_process_directives(yaml_parser_t *parser, SKIP_TOKEN(parser); token = PEEK_TOKEN(parser); - if (!token) goto error; + if (!token) + goto error; } for (default_tag_directive = default_tag_directives; diff --git a/src/reader.c b/src/reader.c index 1691d10f..dc89e647 100644 --- a/src/reader.c +++ b/src/reader.c @@ -103,7 +103,8 @@ yaml_parser_update_raw_buffer(yaml_parser_t *parser) /* Return on EOF. */ - if (parser->eof) return 1; + if (parser->eof) + return 1; /* Move the remaining bytes in the raw buffer to the beginning. */ @@ -183,7 +184,8 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Fill the raw buffer if necessary. */ if (!first || parser->raw_buffer.pointer == parser->raw_buffer.last) { - if (!yaml_parser_update_raw_buffer(parser)) return 0; + if (!yaml_parser_update_raw_buffer(parser)) + return 0; } first = 0; @@ -343,7 +345,7 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Get the character. */ value = parser->raw_buffer.pointer[low] - + (parser->raw_buffer.pointer[high] << 8); + + (parser->raw_buffer.pointer[high] << 8); /* Check for unexpected low surrogate area. */ @@ -409,15 +411,15 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) */ if (! (value == 0x09 || value == 0x0A || value == 0x0D - || (value >= 0x20 && value <= 0x7E) - || (value == 0x85) || (value >= 0xA0 && value <= 0xD7FF) - || (value >= 0xE000 && value <= 0xFFFD) - || (value >= 0x10000 && value <= 0x10FFFF))) { - int err = yaml_parser_set_reader_error(parser, - "control characters are not allowed", - parser->offset, value); + || (value >= 0x20 && value <= 0x7E) + || (value == 0x85) || (value >= 0xA0 && value <= 0xD7FF) + || (value >= 0xE000 && value <= 0xFFFD) + || (value >= 0x10000 && value <= 0x10FFFF))) { + (void)yaml_parser_set_reader_error(parser, + "control characters are not allowed", + parser->offset, value); if (!parser->problem_nonstrict) - return err; + return 0; } /* Move the raw pointers. */ diff --git a/src/scanner.c b/src/scanner.c index 4ae56390..1877c621 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -753,9 +753,9 @@ yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token) /* No tokens after STREAM-END or error. */ - if (parser->stream_end_produced || parser->error) { + if (parser->stream_end_produced + || parser->error) return 1; - } /* Ensure that the tokens queue contains enough tokens. */ From 1c897008c7739349bae817d9084dc8a2bb11955e Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Wed, 9 Mar 2016 11:29:49 +0100 Subject: [PATCH 11/21] implement nonstrict mode optionally allow certian reader errors. needed for perl YAML validation tests. See https://github.com/ingydotnet/yaml-libyaml-pm/issues/44 --- src/parser.c | 6 ++++-- src/scanner.c | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/parser.c b/src/parser.c index 4aaea455..ae55ab61 100644 --- a/src/parser.c +++ b/src/parser.c @@ -179,8 +179,10 @@ yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event) /* No events after the end of the stream or error. */ if (parser->stream_end_produced || - parser->error || - parser->state == YAML_PARSE_END_STATE) { + (parser->error && + /* continue in nonstrict and READER_ERROR */ + (!parser->problem_nonstrict || parser->error != YAML_READER_ERROR)) || + parser->state == YAML_PARSE_END_STATE) { return 1; } diff --git a/src/scanner.c b/src/scanner.c index 1877c621..0fde05b8 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -754,7 +754,9 @@ yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token) /* No tokens after STREAM-END or error. */ if (parser->stream_end_produced - || parser->error) + || (parser->error + /* continue in nonstrict and READER_ERROR */ + && (!parser->problem_nonstrict || parser->error != YAML_READER_ERROR))) return 1; /* Ensure that the tokens queue contains enough tokens. */ From 6e04e03ee2d007109ec52f488d7e28c4d587dcc0 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Wed, 23 Mar 2016 16:18:23 +0100 Subject: [PATCH 12/21] Extend nonstrict mode to unknown escapes also Perl CPAN silently allows any unknown escape chars to pass them to regexp. Mimic that in nonstrict mode. --- src/scanner.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/scanner.c b/src/scanner.c index 0fde05b8..b466ce12 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -3221,9 +3221,15 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, break; default: - yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", - start_mark, "found unknown escape character"); - goto error; + yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", + start_mark, "found unknown escape character"); + if (!parser->problem_nonstrict) { + goto error; + } else { /* all other parsers allow any quoted char, like \. in strings */ + parser->error = YAML_READER_ERROR; /* fake for the YAML_PARSE_END_STATE check */ + *(string.pointer++) = '\\'; + *(string.pointer++) = parser->buffer.pointer[1]; + } } SKIP(parser); From 186ac43298052f83cbdca9e8a72ee3af07f3470e Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Wed, 4 May 2016 08:53:47 +0200 Subject: [PATCH 13/21] fix C++-compat error we cannot malloc to an anon struct in C++. typedef yaml_anchors_t --- include/yaml.h | 21 +++++++++++++-------- src/dumper.c | 2 +- src/emitter.c | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/include/yaml.h b/include/yaml.h index e1b940ba..aa6962ba 100644 --- a/include/yaml.h +++ b/include/yaml.h @@ -1519,6 +1519,18 @@ typedef enum yaml_emitter_state_e { YAML_EMIT_END_STATE } yaml_emitter_state_t; + +/* This is needed for C++ */ + +typedef struct yaml_anchors_s { + /** The number of references. */ + int references; + /** The anchor id. */ + int anchor; + /** If the node has been emitted? */ + int serialized; +} yaml_anchors_t; + /** * The emitter structure. * @@ -1746,14 +1758,7 @@ typedef struct yaml_emitter_s { int closed; /** The information associated with the document nodes. */ - struct { - /** The number of references. */ - int references; - /** The anchor id. */ - int anchor; - /** If the node has been emitted? */ - int serialized; - } *anchors; + yaml_anchors_t *anchors; /** The last assigned anchor id. */ int last_anchor_id; diff --git a/src/dumper.c b/src/dumper.c index 29fb9c07..1fe940b6 100644 --- a/src/dumper.c +++ b/src/dumper.c @@ -131,7 +131,7 @@ yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document) assert(emitter->opened); /* Emitter should be opened. */ - emitter->anchors = yaml_malloc(sizeof(*(emitter->anchors)) + emitter->anchors = (yaml_anchors_t*)yaml_malloc(sizeof(*(emitter->anchors)) * (document->nodes.top - document->nodes.start)); if (!emitter->anchors) goto error; memset(emitter->anchors, 0, sizeof(*(emitter->anchors)) diff --git a/src/emitter.c b/src/emitter.c index 5ea3f673..17de3531 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -16,7 +16,7 @@ #define PUT(emitter,value) \ (FLUSH(emitter) \ && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \ - emitter->column ++, \ + emitter->column++, \ 1)) /* From 569191720abde8777255d0e4d6e2c51da1c073fa Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Thu, 8 Sep 2016 08:49:57 +0200 Subject: [PATCH 14/21] sync with https://bitbucket.org/xi/libyaml --- .hgignore | 35 +++++++++++++++++++++++++++++++++++ .hgtags | 10 ++++++++++ bootstrap-perl | 5 +++++ src/scanner.c | 9 --------- 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 .hgignore create mode 100644 .hgtags create mode 100755 bootstrap-perl diff --git a/.hgignore b/.hgignore new file mode 100644 index 00000000..30f02266 --- /dev/null +++ b/.hgignore @@ -0,0 +1,35 @@ +syntax: glob +aclocal.m4 +autom4te.cache +configure +libtool +Makefile +Makefile.in +stamp-h1 +config +config.h +config.h.in +config.log +config.status +example-deconstructor +example-deconstructor-alt +example-reformatter +example-reformatter-alt +run-dumper +run-emitter +run-loader +run-parser +run-scanner +.deps +.libs +*.o +*.lo +*.la +*.pc + +.git +.gitignore +win32/vc6 +win32/vs2008 +win32/vs2003 +TAGS diff --git a/.hgtags b/.hgtags new file mode 100644 index 00000000..44d7a4e1 --- /dev/null +++ b/.hgtags @@ -0,0 +1,10 @@ +24a3166c80398babe9e92378ca933912b7c63565 0.1.1 +70885badb940fdc774fc2179afbf4a4a16106a49 0.0.1 +754e22e32e2bfe8524633d5241940b903a401441 0.1.3 +754e22e32e2bfe8524633d5241940b903a401441 0.1.4 +d19de7b14cd02c4861e3ed5503c06a48c4ab2e5b 0.1.2 +754e22e32e2bfe8524633d5241940b903a401441 0.1.4 +3e6507fa0c26d20c09f8f468f2bd04aa2fd1b5b5 0.1.4 +a5142b24428b1458513eff73591976eefca1a669 0.1.5 +bce8b60f0b9af69fa9fab3093d0a41ba243de048 0.1.6 +88bd944837cd71cdb55458dad8f43465495455b2 0.1.7 diff --git a/bootstrap-perl b/bootstrap-perl new file mode 100755 index 00000000..06b64290 --- /dev/null +++ b/bootstrap-perl @@ -0,0 +1,5 @@ +#!/bin/sh + +exec autoreconf -fvi +# perl adds a lot of warnings +export CFLAGS='-Wall -Wunused-parameter -Wc++-compat' diff --git a/src/scanner.c b/src/scanner.c index b466ce12..4c7d24dd 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -1110,15 +1110,6 @@ yaml_parser_save_simple_key(yaml_parser_t *parser) int required = (!parser->flow_level && parser->indent == (ptrdiff_t)parser->mark.column); - /* - * A simple key is required only when it is the first token in the current - * line. Therefore it is always allowed. But we add a check anyway. - */ - - /* XXX This caused: - * https://bitbucket.org/xi/libyaml/issue/10/wrapped-strings-cause-assert-failure - assert(parser->simple_key_allowed || !required); */ /* Impossible. */ - /* * If the current position may start a simple key, save it. */ From 6ebdb21d273d74229047f52f44ada9614a271113 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Thu, 8 Sep 2016 08:56:06 +0200 Subject: [PATCH 15/21] add .gitignore --- .gitignore | 35 ++++++++++++++++++++++++----------- .hgignore | 15 ++++++++++++--- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index b497167b..c4271599 100644 --- a/.gitignore +++ b/.gitignore @@ -1,27 +1,40 @@ -aclocal.m4 -autom4te.cache -configure -libtool +*.la +*.lo +*.o +*.pc +.deps +.hg +.libs +CMakeCache.txt +CMakeFiles/ Makefile Makefile.in -stamp-h1 +TAGS +aclocal.m4 +autom4te.cache +cmake_install.cmake config config.h config.h.in config.log config.status +configure example-deconstructor example-deconstructor-alt example-reformatter example-reformatter-alt +libtool +libyaml.a run-dumper run-emitter run-loader run-parser run-scanner -.deps -.libs -*.o -*.lo -*.la -*.pc +stamp-h1 +tests/test-reader +tests/test-reader.log +tests/test-reader.trs +tests/test-suite.log +tests/test-version +tests/test-version.log +tests/test-version.trs diff --git a/.hgignore b/.hgignore index 30f02266..afdae579 100644 --- a/.hgignore +++ b/.hgignore @@ -29,7 +29,16 @@ run-scanner .git .gitignore -win32/vc6 -win32/vs2008 -win32/vs2003 TAGS + +CMakeCache.txt +CMakeFiles/ +cmake_install.cmake +libyaml.a +tests/test-reader +tests/test-reader.log +tests/test-reader.trs +tests/test-suite.log +tests/test-version +tests/test-version.log +tests/test-version.trs From 6319c299a44f0948d670ef227ef44a2dcd79ebe8 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Tue, 13 Sep 2016 15:37:56 +0200 Subject: [PATCH 16/21] fix clang -Wlogical-op warnings --- src/scanner.c | 2 +- src/yaml_private.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/scanner.c b/src/scanner.c index 4c7d24dd..bdb9185b 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -2865,7 +2865,7 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, if (!CACHE(parser, 1)) goto error; - while ((int)parser->mark.column == indent && !IS_Z(parser->buffer)) + while ((int)parser->mark.column == indent && !(IS_Z(parser->buffer))) { /* * We are at the beginning of a non-empty line. diff --git a/src/yaml_private.h b/src/yaml_private.h index bc45f15e..50add9e5 100644 --- a/src/yaml_private.h +++ b/src/yaml_private.h @@ -170,14 +170,14 @@ yaml_string_join( * Check the octet at the specified position. */ -#define CHECK_AT(string,octet,offset) \ - ((string).pointer[offset] == (yaml_char_t)(octet)) +#define CHECK_AT(string,octet,offset) \ + (string).pointer[offset] == (yaml_char_t)(octet) /* * Check the current octet in the buffer. */ -#define CHECK(string,octet) CHECK_AT((string),(octet),0) +#define CHECK(string,octet) (CHECK_AT((string),(octet),0)) /* * Check if the character at the specified position is an alphabetical From 2967ea8c4171dae11c5a20b5c5a5fa84fa1b9a62 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Tue, 13 Sep 2016 16:28:41 +0200 Subject: [PATCH 17/21] fix clang -Wlogical-not-parentheses warnings --- src/api.c | 14 +++++++------- src/emitter.c | 24 ++++++++++++++---------- src/loader.c | 20 ++++++++++---------- src/parser.c | 4 ++-- src/scanner.c | 6 +++--- src/yaml_private.h | 6 +++--- 6 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/api.c b/src/api.c index ee170d87..10732d26 100644 --- a/src/api.c +++ b/src/api.c @@ -218,7 +218,7 @@ yaml_parser_delete(yaml_parser_t *parser) BUFFER_DEL(parser, parser->raw_buffer); BUFFER_DEL(parser, parser->buffer); - while (!QUEUE_EMPTY(parser, parser->tokens)) { + while (!(QUEUE_EMPTY(parser, parser->tokens))) { yaml_token_delete(&DEQUEUE(parser, parser->tokens)); } QUEUE_DEL(parser, parser->tokens); @@ -226,7 +226,7 @@ yaml_parser_delete(yaml_parser_t *parser) STACK_DEL(parser, parser->simple_keys); STACK_DEL(parser, parser->states); STACK_DEL(parser, parser->marks); - while (!STACK_EMPTY(parser, parser->tag_directives)) { + while (!(STACK_EMPTY(parser, parser->tag_directives))) { yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives); yaml_free(tag_directive.handle); yaml_free(tag_directive.prefix); @@ -391,12 +391,12 @@ yaml_emitter_delete(yaml_emitter_t *emitter) BUFFER_DEL(emitter, emitter->buffer); BUFFER_DEL(emitter, emitter->raw_buffer); STACK_DEL(emitter, emitter->states); - while (!QUEUE_EMPTY(emitter, emitter->events)) { + while (!(QUEUE_EMPTY(emitter, emitter->events))) { yaml_event_delete(&DEQUEUE(emitter, emitter->events)); } QUEUE_DEL(emitter, emitter->events); STACK_DEL(emitter, emitter->indents); - while (!STACK_EMPTY(empty, emitter->tag_directives)) { + while (!(STACK_EMPTY(empty, emitter->tag_directives))) { yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives); yaml_free(tag_directive.handle); yaml_free(tag_directive.prefix); @@ -756,7 +756,7 @@ yaml_document_start_event_initialize(yaml_event_t *event, error: yaml_free(version_directive_copy); - while (!STACK_EMPTY(context, tag_directives_copy)) { + while (!(STACK_EMPTY(context, tag_directives_copy))) { yaml_tag_directive_t value = POP(context, tag_directives_copy); yaml_free(value.handle); yaml_free(value.prefix); @@ -1098,7 +1098,7 @@ yaml_document_initialize(yaml_document_t *document, error: STACK_DEL(&context, nodes); yaml_free(version_directive_copy); - while (!STACK_EMPTY(&context, tag_directives_copy)) { + while (!(STACK_EMPTY(&context, tag_directives_copy))) { yaml_tag_directive_t value = POP(&context, tag_directives_copy); yaml_free(value.handle); yaml_free(value.prefix); @@ -1126,7 +1126,7 @@ yaml_document_delete(yaml_document_t *document) assert(document); /* Non-NULL document object is expected. */ - while (!STACK_EMPTY(&context, document->nodes)) { + while (!(STACK_EMPTY(&context, document->nodes))) { yaml_node_t node = POP(&context, document->nodes); yaml_free(node.tag); switch (node.type) { diff --git a/src/emitter.c b/src/emitter.c index 17de3531..fe1490da 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -706,7 +706,7 @@ yaml_emitter_emit_document_end(yaml_emitter_t *emitter, emitter->state = YAML_EMIT_DOCUMENT_START_STATE; - while (!STACK_EMPTY(emitter, emitter->tag_directives)) { + while (!(STACK_EMPTY(emitter, emitter->tag_directives))) { yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives); yaml_free(tag_directive.handle); @@ -1921,9 +1921,11 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, while (string.pointer != string.end) { if (IS_SPACE(string)) { - if (allow_breaks && !spaces - && emitter->column > emitter->best_width - && !IS_SPACE_AT(string, 1)) { + if (allow_breaks + && !spaces + && emitter->column > emitter->best_width + && !(IS_SPACE_AT(string, 1))) + { if (!yaml_emitter_write_indent(emitter)) return 0; MOVE(string); @@ -1988,11 +1990,13 @@ yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, while (string.pointer != string.end) { if (IS_SPACE(string)) { - if (allow_breaks && !spaces - && emitter->column > emitter->best_width - && string.pointer != string.start - && string.pointer != string.end - 1 - && !IS_SPACE_AT(string, 1)) { + if (allow_breaks + && !spaces + && emitter->column > emitter->best_width + && string.pointer != string.start + && string.pointer != string.end - 1 + && !(IS_SPACE_AT(string, 1))) + { if (!yaml_emitter_write_indent(emitter)) return 0; MOVE(string); @@ -2355,7 +2359,7 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, return 0; leading_spaces = IS_BLANK(string); } - if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1) + if (!breaks && IS_SPACE(string) && !(IS_SPACE_AT(string, 1)) && emitter->column > emitter->best_width) { if (!yaml_emitter_write_indent(emitter)) return 0; diff --git a/src/loader.c b/src/loader.c index 59e8b404..191fd9d7 100644 --- a/src/loader.c +++ b/src/loader.c @@ -154,7 +154,7 @@ yaml_parser_set_composer_error_context(yaml_parser_t *parser, static void yaml_parser_delete_aliases(yaml_parser_t *parser) { - while (!STACK_EMPTY(parser, parser->aliases)) { + while (!(STACK_EMPTY(parser, parser->aliases))) { yaml_free(POP(parser, parser->aliases).anchor); } STACK_DEL(parser, parser->aliases); @@ -289,7 +289,7 @@ yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event) int index; yaml_char_t *tag = first_event->data.scalar.tag; - if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) + if (!(STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1))) goto error; if (!tag || strcmp((char *)tag, "!") == 0) { @@ -338,7 +338,7 @@ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event) int index, item_index; yaml_char_t *tag = first_event->data.sequence_start.tag; - if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) + if (!(STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1))) goto error; if (!tag || strcmp((char *)tag, "!") == 0) { @@ -348,7 +348,7 @@ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event) goto error; } - if (!STACK_INIT(parser, items, yaml_node_item_t*)) + if (!(STACK_INIT(parser, items, yaml_node_item_t*))) goto error; SEQUENCE_NODE_INIT(node, tag, items.start, items.end, @@ -367,9 +367,9 @@ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event) if (!yaml_parser_parse(parser, &event)) return 0; while (event.type != YAML_SEQUENCE_END_EVENT) { - if (!STACK_LIMIT(parser, + if (!(STACK_LIMIT(parser, parser->document->nodes.start[index-1].data.sequence.items, - INT_MAX-1)) return 0; + INT_MAX-1))) return 0; item_index = yaml_parser_load_node(parser, &event); if (!item_index) return 0; if (!PUSH(parser, @@ -406,7 +406,7 @@ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event) yaml_node_pair_t pair; yaml_char_t *tag = first_event->data.mapping_start.tag; - if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) + if (!(STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1))) goto error; if (!tag || strcmp((char *)tag, "!") == 0) { @@ -416,7 +416,7 @@ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event) goto error; } - if (!STACK_INIT(parser, pairs, yaml_node_pair_t*)) + if (!(STACK_INIT(parser, pairs, yaml_node_pair_t*))) goto error; MAPPING_NODE_INIT(node, tag, pairs.start, pairs.end, @@ -436,9 +436,9 @@ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event) return 0; while (event.type != YAML_MAPPING_END_EVENT) { - if (!STACK_LIMIT(parser, + if (!(STACK_LIMIT(parser, parser->document->nodes.start[index-1].data.mapping.pairs, - INT_MAX-1)) + INT_MAX-1))) return 0; pair.key = yaml_parser_load_node(parser, &event); if (!pair.key) return 0; diff --git a/src/parser.c b/src/parser.c index ae55ab61..922bfe28 100644 --- a/src/parser.c +++ b/src/parser.c @@ -488,7 +488,7 @@ yaml_parser_parse_document_end(yaml_parser_t *parser, yaml_event_t *event) implicit = 0; } - while (!STACK_EMPTY(parser, parser->tag_directives)) { + while (!(STACK_EMPTY(parser, parser->tag_directives))) { yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives); yaml_free(tag_directive.handle); yaml_free(tag_directive.prefix); @@ -1330,7 +1330,7 @@ yaml_parser_process_directives(yaml_parser_t *parser, error: yaml_free(version_directive); - while (!STACK_EMPTY(parser, tag_directives)) { + while (!(STACK_EMPTY(parser, tag_directives))) { yaml_tag_directive_t tag_directive = POP(parser, tag_directives); yaml_free(tag_directive.handle); yaml_free(tag_directive.prefix); diff --git a/src/scanner.c b/src/scanner.c index bdb9185b..10198fad 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -2183,7 +2183,7 @@ yaml_parser_scan_version_directive_value(yaml_parser_t *parser, /* Eat '.'. */ - if (!CHECK(parser->buffer, '.')) { + if (!(CHECK(parser->buffer, '.'))) { return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", start_mark, "did not find expected digit or '.' character"); } @@ -2420,7 +2420,7 @@ yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token) /* Check for '>' and eat it. */ - if (!CHECK(parser->buffer, '>')) { + if (!(CHECK(parser->buffer, '>'))) { yaml_parser_set_scanner_error(parser, "while scanning a tag", start_mark, "did not find the expected '>'"); goto error; @@ -2514,7 +2514,7 @@ yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive, if (!CACHE(parser, 1)) goto error; - if (!CHECK(parser->buffer, '!')) { + if (!(CHECK(parser->buffer, '!'))) { yaml_parser_set_scanner_error(parser, directive ? "while scanning a tag directive" : "while scanning a tag", start_mark, "did not find expected '!'"); diff --git a/src/yaml_private.h b/src/yaml_private.h index 50add9e5..991be249 100644 --- a/src/yaml_private.h +++ b/src/yaml_private.h @@ -177,7 +177,7 @@ yaml_string_join( * Check the current octet in the buffer. */ -#define CHECK(string,octet) (CHECK_AT((string),(octet),0)) +#define CHECK(string,octet) CHECK_AT((string),(octet),0) /* * Check if the character at the specified position is an alphabetical @@ -432,7 +432,7 @@ yaml_queue_extend(void **start, void **head, void **tail, void **end); (stack).start = (stack).top = (stack).end = 0) #define STACK_EMPTY(context,stack) \ - ((stack).start == (stack).top) + (stack).start == (stack).top #define STACK_LIMIT(context,stack,size) \ ((stack).top - (stack).start < (size) ? \ @@ -465,7 +465,7 @@ yaml_queue_extend(void **start, void **head, void **tail, void **end); (queue).start = (queue).head = (queue).tail = (queue).end = 0) #define QUEUE_EMPTY(context,queue) \ - ((queue).head == (queue).tail) + (queue).head == (queue).tail #define ENQUEUE(context,queue,value) \ (((queue).tail != (queue).end \ From 710af8a524443f224b63ecd442b50436a137deaf Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Fri, 14 Oct 2016 22:30:42 +0200 Subject: [PATCH 18/21] api: yaml_document_delete remove unneeded context, which is not set in any macro. --- include/yaml.h | 2 +- src/api.c | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/include/yaml.h b/include/yaml.h index aa6962ba..20616b1b 100644 --- a/include/yaml.h +++ b/include/yaml.h @@ -95,7 +95,7 @@ typedef struct yaml_tag_directive_s { /** The stream encoding. */ typedef enum yaml_encoding_e { - /** Let the parser choose the encoding. */ + /** Let the parser choose the encoding. The initial state, invalid at runtime. */ YAML_ANY_ENCODING, /** The default UTF-8 encoding. */ YAML_UTF8_ENCODING, diff --git a/src/api.c b/src/api.c index 10732d26..b9e325e7 100644 --- a/src/api.c +++ b/src/api.c @@ -1117,13 +1117,8 @@ yaml_document_initialize(yaml_document_t *document, YAML_DECLARE(void) yaml_document_delete(yaml_document_t *document) { - struct { - yaml_error_type_t error; - } context; yaml_tag_directive_t *tag_directive; - context.error = YAML_NO_ERROR; /* Eliminate a compliler warning. */ - assert(document); /* Non-NULL document object is expected. */ while (!(STACK_EMPTY(&context, document->nodes))) { From 5bb0f3da03fa0fe1caa5dbc5f4a9192978ecab13 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Fri, 14 Oct 2016 22:31:15 +0200 Subject: [PATCH 19/21] emitter: new PUT_BREAK_void macro for void context, to fix -Wunused-value warnings --- src/emitter.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/emitter.c b/src/emitter.c index fe1490da..2d16a93d 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -36,6 +36,20 @@ emitter->line ++, \ 1)) +/* returning nothing */ + +#define PUT_BREAK_void(emitter) \ + (emitter->line_break == YAML_CR_BREAK ? \ + (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \ + emitter->line_break == YAML_LN_BREAK ? \ + (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \ + emitter->line_break == YAML_CRLN_BREAK ? \ + (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \ + *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0, \ + emitter->column = 0, \ + emitter->line ++ \ + ) + /* * Copy a character from a string into buffer. */ @@ -53,7 +67,7 @@ #define WRITE_BREAK(emitter,string) \ (FLUSH(emitter) \ && (CHECK(string,'\n') ? \ - (PUT_BREAK(emitter), \ + (PUT_BREAK_void(emitter), \ string.pointer ++, \ 1) : \ (COPY(emitter->buffer,string), \ From 1fe837d757dc2d17ce954589f076b13fad37f96d Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Sun, 20 Nov 2016 17:55:52 +0100 Subject: [PATCH 20/21] fix C++ g++-6 errors yaml_emitter_write_indicator const char *indicator --- src/emitter.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/emitter.c b/src/emitter.c index 2d16a93d..ff2687cf 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -235,7 +235,7 @@ yaml_emitter_write_indent(yaml_emitter_t *emitter); static int yaml_emitter_write_indicator(yaml_emitter_t *emitter, - char *indicator, int need_whitespace, + const char *indicator, int need_whitespace, int is_whitespace, int is_indention); static int @@ -1799,7 +1799,7 @@ yaml_emitter_write_indent(yaml_emitter_t *emitter) static int yaml_emitter_write_indicator(yaml_emitter_t *emitter, - char *indicator, int need_whitespace, + const char *indicator, int need_whitespace, int is_whitespace, int is_indention) { size_t indicator_length; @@ -2240,7 +2240,7 @@ yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, yaml_string_t string) { char indent_hint[2]; - char *chomp_hint = NULL; + const char *chomp_hint = NULL; if (IS_SPACE(string) || IS_BREAK(string)) { indent_hint[0] = '0' + (char)emitter->best_indent; From abaf719330e742d80c7295c6943fe14c31ec83bd Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Thu, 30 Mar 2017 09:49:15 +0200 Subject: [PATCH 21/21] fix version_directive memory leak detected by coverity --- src/parser.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/parser.c b/src/parser.c index 922bfe28..ee28211e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1326,6 +1326,7 @@ yaml_parser_process_directives(yaml_parser_t *parser, STACK_DEL(parser, tag_directives); } + yaml_free(version_directive); return 1; error: