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

audit manually specified string literal lengths #4

Merged
merged 1 commit into from
Oct 3, 2023
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
34 changes: 18 additions & 16 deletions src/emitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,27 +258,29 @@ static bool _emit_identifier(kdl_emitter *self, kdl_str name)
}
}

#define _write_string_literal_ok(self, s) (self->write_func(self->write_user_data, ("" s ""), sizeof(s)-1) == sizeof(s)-1)

static bool _emit_value(kdl_emitter *self, kdl_value const* v)
{
if (v->type_annotation.data != NULL)
{
if ((self->write_func(self->write_user_data, "(", 1) != 1)
|| !_emit_identifier(self, v->type_annotation)
|| (self->write_func(self->write_user_data, ")", 1) != 1))
if (!(_write_string_literal_ok(self, "(")
&& _emit_identifier(self, v->type_annotation)
&& _write_string_literal_ok(self, ")")))
return false;
}
switch (v->type) {
case KDL_TYPE_NULL:
return self->write_func(self->write_user_data, "null", 4) == 4;
return _write_string_literal_ok(self, "null");
case KDL_TYPE_STRING:
return _emit_str(self, v->string);
case KDL_TYPE_NUMBER:
return _emit_number(self, &v->number);
case KDL_TYPE_BOOLEAN:
if (v->boolean) {
return self->write_func(self->write_user_data, "true", 4) == 4;
return _write_string_literal_ok(self, "true");
} else {
return self->write_func(self->write_user_data, "false", 5) == 5;
return _write_string_literal_ok(self, "false");
}
}
return false;
Expand All @@ -287,14 +289,14 @@ static bool _emit_value(kdl_emitter *self, kdl_value const* v)
static bool _emit_node_preamble(kdl_emitter *self)
{
if (!self->start_of_line) {
if (self->write_func(self->write_user_data, "\n", 1) != 1) return false;
if (!_write_string_literal_ok(self, "\n")) return false;
}
int indent = 0;
for (int d = 0; d < self->depth; ++d) {
indent += self->opt.indent;
}
for (int i = 0; i < indent; ++i) {
if (self->write_func(self->write_user_data, " ", 1) != 1) return false;
if (!_write_string_literal_ok(self, " ")) return false;
}
self->start_of_line = false;
return true;
Expand All @@ -308,31 +310,31 @@ bool kdl_emit_node(kdl_emitter *self, kdl_str name)
bool kdl_emit_node_with_type(kdl_emitter *self, kdl_str type, kdl_str name)
{
return _emit_node_preamble(self)
&& (self->write_func(self->write_user_data, "(", 1) == 1)
&& _write_string_literal_ok(self, "(")
&& _emit_identifier(self, type)
&& (self->write_func(self->write_user_data, ")", 1) == 1)
&& _write_string_literal_ok(self, ")")
&& _emit_identifier(self, name);
}

bool kdl_emit_arg(kdl_emitter *self, kdl_value const *value)
{
return (self->write_func(self->write_user_data, " ", 1) == 1)
return _write_string_literal_ok(self, " ")
&& _emit_value(self, value);
}

bool kdl_emit_property(kdl_emitter *self, kdl_str name, kdl_value const *value)
{
return (self->write_func(self->write_user_data, " ", 1) == 1)
return (_write_string_literal_ok(self, " "))
&& _emit_identifier(self, name)
&& (self->write_func(self->write_user_data, "=", 1) == 1)
&& _write_string_literal_ok(self, "=")
&& _emit_value(self, value);
}

bool kdl_start_emitting_children(kdl_emitter *self)
{
self->start_of_line = true;
++self->depth;
return (self->write_func(self->write_user_data, " {\n", 3) == 3);
return (_write_string_literal_ok(self, " {\n"));
}

bool kdl_finish_emitting_children(kdl_emitter *self)
Expand All @@ -341,7 +343,7 @@ bool kdl_finish_emitting_children(kdl_emitter *self)
--self->depth;
if (!_emit_node_preamble(self)) return false;
self->start_of_line = true;
return (self->write_func(self->write_user_data, "}\n", 2) == 2);
return (_write_string_literal_ok(self, "}\n"));
}

bool kdl_emit_end(kdl_emitter *self)
Expand All @@ -350,7 +352,7 @@ bool kdl_emit_end(kdl_emitter *self)
if (!kdl_finish_emitting_children(self)) return false;
}
if (!self->start_of_line) {
if (self->write_func(self->write_user_data, "\n", 1) != 1) return false;
if (!_write_string_literal_ok(self, "\n")) return false;
self->start_of_line = true;
}
return true;
Expand Down
20 changes: 10 additions & 10 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <stdio.h>
#include <math.h>

#define _str_equals_literal(k, l) ((k).len == (sizeof(l "") - 1) && 0 == memcmp(("" l), (k).data, (sizeof(l) - 1)))

enum _kdl_parser_state {
// Basic states
PARSER_OUTSIDE_NODE,
Expand Down Expand Up @@ -535,16 +537,14 @@ static bool _parse_value(kdl_token const *token, kdl_value *val, kdl_owned_strin
return true;
}
case KDL_TOKEN_WORD:
if (token->value.len == 4) {
if (memcmp("null", token->value.data, 4) == 0) {
val->type = KDL_TYPE_NULL;
return true;
} else if (memcmp("true", token->value.data, 4) == 0) {
val->type = KDL_TYPE_BOOLEAN;
val->boolean = true;
return true;
}
} else if (token->value.len == 5 && memcmp("false", token->value.data, 4) == 0) {
if (_str_equals_literal(token->value, "null")) {
val->type = KDL_TYPE_NULL;
return true;
} else if (_str_equals_literal(token->value, "true")) {
val->type = KDL_TYPE_BOOLEAN;
val->boolean = true;
return true;
} else if (_str_equals_literal(token->value, "false")) {
val->type = KDL_TYPE_BOOLEAN;
val->boolean = false;
return true;
Expand Down