Skip to content

Commit d1bfe61

Browse files
committed
parse: Do not initialize the content in context to NULL
String operations in libgit2 are supposed to never receive `NULL`, e.g. they are not `NULL`-save. In the case of `git__linenlen()`, invocation with `NULL` leads to undefined behavior. In a `git_parse_ctx` however, the `content` field used in these operations was initialized to `NULL` if the `git_parse_ctx_init()` was called with `NULL` for `content` or `0` for `content_len`. For the latter case, the initialization function even contained some logic for initializing `content` with `NULL`. This commit mitigates triggering undefined behavior by rewriting the logic. Now `content` is always initialized to a non-null buffer. Instead of a null buffer, an empty string is used for denoting an empty buffer.
1 parent 64138b7 commit d1bfe61

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/parse.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len)
1010
{
11-
if (content_len)
11+
if (content && content_len) {
1212
ctx->content = content;
13-
else
14-
ctx->content = NULL;
13+
ctx->content_len = content_len;
14+
} else {
15+
ctx->content = "";
16+
ctx->content_len = 0;
17+
}
1518

16-
ctx->content_len = content_len;
1719
ctx->remain = ctx->content;
1820
ctx->remain_len = ctx->content_len;
1921
ctx->line = ctx->remain;
@@ -26,6 +28,7 @@ int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_l
2628
void git_parse_ctx_clear(git_parse_ctx *ctx)
2729
{
2830
memset(ctx, 0, sizeof(*ctx));
31+
ctx->content = "";
2932
}
3033

3134
void git_parse_advance_line(git_parse_ctx *ctx)

0 commit comments

Comments
 (0)