@@ -116,6 +116,24 @@ static cmark_strbuf *unescape_pipes(cmark_mem *mem, unsigned char *string, bufsi
116
116
return res ;
117
117
}
118
118
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
+
119
137
static table_row * row_from_string (cmark_syntax_extension * self ,
120
138
cmark_parser * parser , unsigned char * string ,
121
139
int len ) {
@@ -157,20 +175,11 @@ static table_row *row_from_string(cmark_syntax_extension *self,
157
175
cell_matched );
158
176
cmark_strbuf_trim (cell_buf );
159
177
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 ;
171
182
}
172
- row -> n_columns = n_columns ;
173
- node_cell * cell = & row -> cells [n_columns - 1 ];
174
183
cell -> buf = cell_buf ;
175
184
cell -> start_offset = offset ;
176
185
cell -> end_offset = offset + cell_matched - 1 ;
0 commit comments