Skip to content

Commit 6e8f0bf

Browse files
committed
Fixed undefined shift in commonmark writer.
Closes commonmark#211. Found by google/oss-fuzz: https://oss-fuzz.com/v2/testcase-detail/4686992824598528
1 parent f8737b1 commit 6e8f0bf

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/commonmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <string.h>
4+
#include <stdint.h>
45
#include <assert.h>
56

67
#include "config.h"
@@ -81,15 +82,17 @@ static int longest_backtick_sequence(const char *code) {
8182
}
8283

8384
static int shortest_unused_backtick_sequence(const char *code) {
84-
int32_t used = 1;
85+
// note: if the shortest sequence is >= 32, this returns 32
86+
// so as not to overflow the bit array.
87+
uint32_t used = 1;
8588
int current = 0;
8689
size_t i = 0;
8790
size_t code_len = strlen(code);
8891
while (i <= code_len) {
8992
if (code[i] == '`') {
9093
current++;
9194
} else {
92-
if (current) {
95+
if (current > 0 && current < 32) {
9396
used |= (1 << current);
9497
}
9598
current = 0;
@@ -98,7 +101,7 @@ static int shortest_unused_backtick_sequence(const char *code) {
98101
}
99102
// return number of first bit that is 0:
100103
i = 0;
101-
while (used & 1) {
104+
while (i < 32 && used & 1) {
102105
used = used >> 1;
103106
i++;
104107
}

0 commit comments

Comments
 (0)