Skip to content

Commit 016cf82

Browse files
committed
Prevent implicit integer truncations
This commit changes size integers to consistently be of type size_t to prevent implicit integer truncations during integer arithmetic. What remain are explicit truncations to (bufsize_t) which is of type int32_t. This requires additional careful consideration.
1 parent ad38ac0 commit 016cf82

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

extensions/autolink.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
#include <strings.h>
1010
#endif
1111

12+
// for ssize_t
13+
#ifdef _MSC_VER
14+
#include <BaseTsd.h>
15+
typedef SSIZE_T ssize_t;
16+
#else
17+
#include <unistd.h>
18+
#endif
19+
1220
static int is_valid_hostchar(const uint8_t *link, size_t link_len) {
1321
int32_t ch;
1422
int r = cmark_utf8proc_iterate(link, (bufsize_t)link_len, &ch);
@@ -290,10 +298,10 @@ static cmark_node *match(cmark_syntax_extension *ext, cmark_parser *parser,
290298
// inline was finished in inlines.c.
291299
}
292300

293-
static bool validate_protocol(char protocol[], uint8_t *data, int rewind, int max_rewind) {
301+
static bool validate_protocol(char protocol[], uint8_t *data, size_t rewind, size_t max_rewind) {
294302
size_t len = strlen(protocol);
295303

296-
if (len > (size_t)(max_rewind - rewind)) {
304+
if (len > (max_rewind - rewind)) {
297305
return false;
298306
}
299307

@@ -302,11 +310,11 @@ static bool validate_protocol(char protocol[], uint8_t *data, int rewind, int ma
302310
return false;
303311
}
304312

305-
if (len == (size_t)(max_rewind - rewind)) {
313+
if (len == (max_rewind - rewind)) {
306314
return true;
307315
}
308316

309-
char prev_char = data[-rewind - len - 1];
317+
char prev_char = data[-((ssize_t)rewind) - len - 1];
310318

311319
// Make sure the character before the protocol is non-alphanumeric
312320
return !cmark_isalnum(prev_char);
@@ -421,7 +429,7 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
421429
cmark_node *link_text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
422430
cmark_chunk email = cmark_chunk_dup(
423431
&detached_chunk,
424-
start + offset + max_rewind - rewind,
432+
(bufsize_t)(start + offset + max_rewind - rewind),
425433
(bufsize_t)(link_end + rewind));
426434
cmark_chunk_to_cstr(parser->mem, &email);
427435
link_text->as.literal = email;
@@ -436,7 +444,7 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
436444

437445
cmark_node_insert_after(link_node, post);
438446

439-
text->as.literal = cmark_chunk_dup(&detached_chunk, start, offset + max_rewind - rewind);
447+
text->as.literal = cmark_chunk_dup(&detached_chunk, (bufsize_t)start, (bufsize_t)(offset + max_rewind - rewind));
440448
cmark_chunk_to_cstr(parser->mem, &text->as.literal);
441449

442450
text = post;

extensions/table.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static node_cell* append_row_cell(cmark_mem *mem, table_row *row) {
133133
// Use realloc to double the size of the buffer.
134134
row->cells = (node_cell *)mem->realloc(row->cells, (2 * n_columns - 1) * sizeof(node_cell));
135135
}
136-
row->n_columns = n_columns;
136+
row->n_columns = (uint16_t)n_columns;
137137
return &row->cells[n_columns-1];
138138
}
139139

src/map.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ refsearch(const void *label, const void *p2) {
5151
}
5252

5353
static void sort_map(cmark_map *map) {
54-
unsigned int i = 0, last = 0, size = map->size;
54+
size_t i = 0, last = 0, size = map->size;
5555
cmark_map_entry *r = map->refs, **sorted = NULL;
5656

5757
sorted = (cmark_map_entry **)map->mem->calloc(size, sizeof(cmark_map_entry *));

src/map.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ extern "C" {
1010
struct cmark_map_entry {
1111
struct cmark_map_entry *next;
1212
unsigned char *label;
13-
unsigned int age;
14-
unsigned int size;
13+
size_t age;
14+
size_t size;
1515
};
1616

1717
typedef struct cmark_map_entry cmark_map_entry;
@@ -24,9 +24,9 @@ struct cmark_map {
2424
cmark_mem *mem;
2525
cmark_map_entry *refs;
2626
cmark_map_entry **sorted;
27-
unsigned int size;
28-
unsigned int ref_size;
29-
unsigned int max_ref_size;
27+
size_t size;
28+
size_t ref_size;
29+
size_t max_ref_size;
3030
cmark_map_free_f free;
3131
};
3232

src/parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct cmark_parser {
4646
/* Options set by the user, see the Options section in cmark.h */
4747
int options;
4848
bool last_buffer_ended_with_cr;
49-
unsigned int total_size;
49+
size_t total_size;
5050
cmark_llist *syntax_extensions;
5151
cmark_llist *inline_syntax_extensions;
5252
cmark_ispunct_func backslash_ispunct;

0 commit comments

Comments
 (0)