Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Fix segmentations failure in error.c gumbo_caret_diagnostic_to_string #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ gumbo_test_SOURCES = \
tests/tokenizer.cc \
tests/test_utils.cc \
tests/utf8.cc \
tests/vector.cc
tests/vector.cc \
tests/error.cc
gumbo_test_DEPENDENCIES = libgumbo.la
gumbo_test_LDADD = libgumbo.la

Expand Down
2 changes: 1 addition & 1 deletion src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static const char* find_last_newline(
// There may be an error at EOF, which would be a nul byte.
assert(*c || c == error_location);
}
return c == original_text ? c : c + 1;
return c == original_text || c == error_location ? c : c + 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually the correct fix? If *error_location is \n, shouldn't this move to the previous \n (if any)? For example, given the source text "<\n", that line is the one with the error. With your patch, wouldn't original_line in gumbo_caret_diagnostic_to_string() point to the new line and have length 0?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you're right. Actually i did not care about error message and did not thinking about that. But i think that is are edge case and handling of that case should be done in caller function.

}

// Finds the next newline in the original source buffer from a given byte
Expand Down
41 changes: 41 additions & 0 deletions tests/error.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "gumbo.h"
#include "parser.h"
#include "error.h"

#include <string>

#include "gtest/gtest.h"
#include "test_utils.h"

namespace {

class GumboErrorTest : public ::testing::Test {
protected:
GumboErrorTest() {}

virtual ~GumboErrorTest() {

}
};

TEST_F(GumboErrorTest, NewlineAfterLessThanSymbol) {
const GumboOptions *options = &kGumboDefaultOptions;
const char *input = "<\n";
size_t input_len = strlen(input);
GumboOutput *output = gumbo_parse_with_options(options, input, input_len);
GumboVector *errors = &output->errors;
GumboParser parser = { ._options = options };
GumboStringBuffer msg;

gumbo_string_buffer_init(&parser, &msg);
for (size_t i=0; i < errors->length; i++) {
GumboError *err = (GumboError *)errors->data[i];
gumbo_string_buffer_clear(&parser, &msg);
gumbo_caret_diagnostic_to_string(&parser, err, input, &msg);
}
gumbo_string_buffer_destroy(&parser, &msg);

gumbo_destroy_output(options, output);
}

}