Skip to content

Commit a1129ab

Browse files
Refactor cell append code into a separate function.
1 parent 78e3f1d commit a1129ab

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

extensions/table.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ static cmark_strbuf *unescape_pipes(cmark_mem *mem, unsigned char *string, bufsi
116116
return res;
117117
}
118118

119+
// Adds a new cell to the end of the row. A pointer to the new cell is returned
120+
// for the caller to initialize.
121+
static node_cell* append_row_cell(cmark_mem *mem, table_row *row) {
122+
const uint32_t n_columns = row->n_columns + 1;
123+
// realloc when n_columns is a power of 2
124+
if ((n_columns & (n_columns-1)) == 0) {
125+
// make sure we never wrap row->n_columns
126+
// offset will != len and our exit will clean up as intended
127+
if (n_columns > UINT16_MAX) {
128+
return NULL;
129+
}
130+
// Use realloc to double the size of the buffer.
131+
row->cells = (node_cell *)mem->realloc(row->cells, (2 * n_columns - 1) * sizeof(node_cell));
132+
}
133+
row->n_columns = n_columns;
134+
return &row->cells[n_columns-1];
135+
}
136+
119137
static table_row *row_from_string(cmark_syntax_extension *self,
120138
cmark_parser *parser, unsigned char *string,
121139
int len) {
@@ -157,20 +175,11 @@ static table_row *row_from_string(cmark_syntax_extension *self,
157175
cell_matched);
158176
cmark_strbuf_trim(cell_buf);
159177

160-
const uint32_t n_columns = row->n_columns + 1;
161-
// realloc when n_columns is a power of 2
162-
if ((n_columns & (n_columns-1)) == 0) {
163-
// make sure we never wrap row->n_columns
164-
// offset will != len and our exit will clean up as intended
165-
if (n_columns > UINT16_MAX) {
166-
int_overflow_abort = 1;
167-
break;
168-
}
169-
// Use realloc to double the size of the buffer.
170-
row->cells = (node_cell *)parser->mem->realloc(row->cells, (2 * n_columns - 1) * sizeof(node_cell));
178+
node_cell *cell = append_row_cell(parser->mem, row);
179+
if (!cell) {
180+
int_overflow_abort = 1;
181+
break;
171182
}
172-
row->n_columns = n_columns;
173-
node_cell *cell = &row->cells[n_columns-1];
174183
cell->buf = cell_buf;
175184
cell->start_offset = offset;
176185
cell->end_offset = offset + cell_matched - 1;

0 commit comments

Comments
 (0)