Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialize buffer size check #47

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
11 changes: 7 additions & 4 deletions dialects/terraform/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ typedef struct {
String string_new() { return (String){.cap = 16, .len = 0, .data = calloc(1, sizeof(char) * 17)}; }

typedef struct {
enum ContextType type;
ContextType type;

// valid if type == HEREDOC_TEMPLATE
String heredoc_identifier;
Expand Down Expand Up @@ -131,13 +131,14 @@ static unsigned serialize(Scanner *scanner, char *buf) {
size += sizeof(uint32_t);
for (int i = 0; i < scanner->context_stack.len; i++) {
Context *context = &scanner->context_stack.data[i];
if (size + 2 + context->heredoc_identifier.len >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
if (size + sizeof(ContextType) + sizeof(uint32_t) + context->heredoc_identifier.len >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
return 0;
}
if (context->heredoc_identifier.len > CHAR_MAX) {
return 0;
}
buf[size++] = context->type;
memcpy(&buf[size], &(context->type), sizeof(ContextType));
size += sizeof(ContextType);
memcpy(&buf[size], &(context->heredoc_identifier.len), sizeof(uint32_t));
size += sizeof(uint32_t);
memcpy(&buf[size], context->heredoc_identifier.data, context->heredoc_identifier.len);
Expand All @@ -160,7 +161,9 @@ static void deserialize(Scanner *scanner, const char *buffer, unsigned length) {
for (uint32_t j = 0; j < context_stack_size; j++) {
Context ctx;
ctx.heredoc_identifier = string_new();
ctx.type = (enum ContextType)buffer[size++];

memcpy(&(ctx.type), &buffer[size], sizeof(ContextType));
size += sizeof(ContextType);

uint32_t heredoc_identifier_size;
memcpy(&heredoc_identifier_size, &buffer[size], sizeof(uint32_t));
Expand Down
15 changes: 9 additions & 6 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ enum TokenType {
HEREDOC_IDENTIFIER,
};

enum ContextType {
typedef enum ContextType {
TEMPLATE_INTERPOLATION,
TEMPLATE_DIRECTIVE,
QUOTED_TEMPLATE,
HEREDOC_TEMPLATE,
};
} ContextType;
Copy link
Contributor

Choose a reason for hiding this comment

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

You're missing these changes in the terraform scanner.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🙈 that explains it; sorry and thank you! Is there a way to handle the terraform scanner more elegantly? I put it there because at the time nvim-treesitter didnt allow having queries for multiple filetypes with one parser.

Copy link
Contributor

@clason clason Jun 17, 2024

Choose a reason for hiding this comment

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

If it's really the same parser, you can register it for other filetypes with vim.treesitter.language.register(). But if the queries differ, this is pretty much the only way without manual query loading.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Queries differ somewhat ( there used to be only one, but then there were some complaints that some strings got highlighted as terraform keywords in other hcl based languages like for packer or whatnot, so i split it into generic hcl and then terraform iirc )

Copy link
Contributor

Choose a reason for hiding this comment

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

Then I think this is the best for now (until upstream tree-sitter adds dedicated "dialect" support).


typedef struct {
uint32_t cap;
Expand All @@ -100,7 +100,7 @@ typedef struct {
String string_new() { return (String){.cap = 16, .len = 0, .data = calloc(1, sizeof(char) * 17)}; }

typedef struct {
enum ContextType type;
ContextType type;

// valid if type == HEREDOC_TEMPLATE
String heredoc_identifier;
Expand Down Expand Up @@ -131,13 +131,14 @@ static unsigned serialize(Scanner *scanner, char *buf) {
size += sizeof(uint32_t);
for (int i = 0; i < scanner->context_stack.len; i++) {
Context *context = &scanner->context_stack.data[i];
if (size + 2 + context->heredoc_identifier.len >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
if (size + sizeof(ContextType) + sizeof(uint32_t) + context->heredoc_identifier.len >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
return 0;
}
if (context->heredoc_identifier.len > CHAR_MAX) {
return 0;
}
buf[size++] = context->type;
memcpy(&buf[size], &(context->type), sizeof(ContextType));
size += sizeof(ContextType);
memcpy(&buf[size], &(context->heredoc_identifier.len), sizeof(uint32_t));
size += sizeof(uint32_t);
memcpy(&buf[size], context->heredoc_identifier.data, context->heredoc_identifier.len);
Expand All @@ -160,7 +161,9 @@ static void deserialize(Scanner *scanner, const char *buffer, unsigned length) {
for (uint32_t j = 0; j < context_stack_size; j++) {
Context ctx;
ctx.heredoc_identifier = string_new();
ctx.type = (enum ContextType)buffer[size++];

memcpy(&(ctx.type), &buffer[size], sizeof(ContextType));
size += sizeof(ContextType);

uint32_t heredoc_identifier_size;
memcpy(&heredoc_identifier_size, &buffer[size], sizeof(uint32_t));
Expand Down
Loading