@@ -186,18 +186,22 @@ static int obj_index_tree_node_compare(const void *a, const void *b)
186
186
& ((const struct obj_index_tree_node * )b )-> hash );
187
187
}
188
188
189
- static void writer_index_hash (struct reftable_writer * w , struct strbuf * hash )
189
+ static int writer_index_hash (struct reftable_writer * w , struct strbuf * hash )
190
190
{
191
191
uint64_t off = w -> next ;
192
-
193
192
struct obj_index_tree_node want = { .hash = * hash };
193
+ struct obj_index_tree_node * key ;
194
+ struct tree_node * node ;
194
195
195
- struct tree_node * node = tree_search (& want , & w -> obj_index_tree ,
196
- & obj_index_tree_node_compare , 0 );
197
- struct obj_index_tree_node * key = NULL ;
196
+ node = tree_search (& want , & w -> obj_index_tree ,
197
+ & obj_index_tree_node_compare , 0 );
198
198
if (!node ) {
199
199
struct obj_index_tree_node empty = OBJ_INDEX_TREE_NODE_INIT ;
200
- key = reftable_malloc (sizeof (struct obj_index_tree_node ));
200
+
201
+ key = reftable_malloc (sizeof (* key ));
202
+ if (!key )
203
+ return REFTABLE_OUT_OF_MEMORY_ERROR ;
204
+
201
205
* key = empty ;
202
206
203
207
strbuf_reset (& key -> hash );
@@ -208,12 +212,15 @@ static void writer_index_hash(struct reftable_writer *w, struct strbuf *hash)
208
212
key = node -> key ;
209
213
}
210
214
211
- if (key -> offset_len > 0 && key -> offsets [key -> offset_len - 1 ] == off ) {
212
- return ;
213
- }
215
+ if (key -> offset_len > 0 && key -> offsets [key -> offset_len - 1 ] == off )
216
+ return 0 ;
214
217
215
218
REFTABLE_ALLOC_GROW (key -> offsets , key -> offset_len + 1 , key -> offset_cap );
219
+ if (!key -> offsets )
220
+ return REFTABLE_OUT_OF_MEMORY_ERROR ;
216
221
key -> offsets [key -> offset_len ++ ] = off ;
222
+
223
+ return 0 ;
217
224
}
218
225
219
226
static int writer_add_record (struct reftable_writer * w ,
@@ -284,36 +291,44 @@ int reftable_writer_add_ref(struct reftable_writer *w,
284
291
.ref = * ref
285
292
},
286
293
};
287
- int err = 0 ;
294
+ struct strbuf buf = STRBUF_INIT ;
295
+ int err ;
288
296
289
- if (!ref -> refname )
290
- return REFTABLE_API_ERROR ;
291
- if (ref -> update_index < w -> min_update_index ||
297
+ if (!ref -> refname ||
298
+ ref -> update_index < w -> min_update_index ||
292
299
ref -> update_index > w -> max_update_index )
293
300
return REFTABLE_API_ERROR ;
294
301
295
302
rec .u .ref .update_index -= w -> min_update_index ;
296
303
297
304
err = writer_add_record (w , & rec );
298
305
if (err < 0 )
299
- return err ;
306
+ goto out ;
300
307
301
308
if (!w -> opts .skip_index_objects && reftable_ref_record_val1 (ref )) {
302
- struct strbuf h = STRBUF_INIT ;
303
- strbuf_add (& h , (char * )reftable_ref_record_val1 (ref ),
309
+ strbuf_add (& buf , (char * )reftable_ref_record_val1 (ref ),
304
310
hash_size (w -> opts .hash_id ));
305
- writer_index_hash (w , & h );
306
- strbuf_release (& h );
311
+
312
+ err = writer_index_hash (w , & buf );
313
+ if (err < 0 )
314
+ goto out ;
307
315
}
308
316
309
317
if (!w -> opts .skip_index_objects && reftable_ref_record_val2 (ref )) {
310
- struct strbuf h = STRBUF_INIT ;
311
- strbuf_add (& h , reftable_ref_record_val2 (ref ),
318
+ strbuf_reset ( & buf ) ;
319
+ strbuf_add (& buf , reftable_ref_record_val2 (ref ),
312
320
hash_size (w -> opts .hash_id ));
313
- writer_index_hash (w , & h );
314
- strbuf_release (& h );
321
+
322
+ err = writer_index_hash (w , & buf );
323
+ if (err < 0 )
324
+ goto out ;
315
325
}
316
- return 0 ;
326
+
327
+ err = 0 ;
328
+
329
+ out :
330
+ strbuf_release (& buf );
331
+ return err ;
317
332
}
318
333
319
334
int reftable_writer_add_refs (struct reftable_writer * w ,
0 commit comments