Skip to content

Commit 561240d

Browse files
authored
Merge pull request commonmark#302 from github/fix-size-integers-01232023
Prevent implicit integer truncations
2 parents ad38ac0 + 01162c5 commit 561240d

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

extensions/autolink.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <parser.h>
33
#include <string.h>
44
#include <utf8.h>
5+
#include <stddef.h>
56

67
#if defined(_WIN32)
78
#define strncasecmp _strnicmp
@@ -290,10 +291,10 @@ static cmark_node *match(cmark_syntax_extension *ext, cmark_parser *parser,
290291
// inline was finished in inlines.c.
291292
}
292293

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

296-
if (len > (size_t)(max_rewind - rewind)) {
297+
if (len > (max_rewind - rewind)) {
297298
return false;
298299
}
299300

@@ -302,11 +303,11 @@ static bool validate_protocol(char protocol[], uint8_t *data, int rewind, int ma
302303
return false;
303304
}
304305

305-
if (len == (size_t)(max_rewind - rewind)) {
306+
if (len == (max_rewind - rewind)) {
306307
return true;
307308
}
308309

309-
char prev_char = data[-rewind - len - 1];
310+
char prev_char = data[-((ptrdiff_t)rewind) - len - 1];
310311

311312
// Make sure the character before the protocol is non-alphanumeric
312313
return !cmark_isalnum(prev_char);
@@ -421,7 +422,7 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
421422
cmark_node *link_text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
422423
cmark_chunk email = cmark_chunk_dup(
423424
&detached_chunk,
424-
start + offset + max_rewind - rewind,
425+
(bufsize_t)(start + offset + max_rewind - rewind),
425426
(bufsize_t)(link_end + rewind));
426427
cmark_chunk_to_cstr(parser->mem, &email);
427428
link_text->as.literal = email;
@@ -436,7 +437,7 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
436437

437438
cmark_node_insert_after(link_node, post);
438439

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

442443
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)