@@ -92,16 +92,15 @@ static pthread_cond_t cond_result;
92
92
93
93
static int skip_first_line ;
94
94
95
- static void add_work (struct grep_opt * opt , enum grep_source_type type ,
96
- const char * name , const char * path , const void * id )
95
+ static void add_work (struct grep_opt * opt , const struct grep_source * gs )
97
96
{
98
97
grep_lock ();
99
98
100
99
while ((todo_end + 1 ) % ARRAY_SIZE (todo ) == todo_done ) {
101
100
pthread_cond_wait (& cond_write , & grep_mutex );
102
101
}
103
102
104
- grep_source_init ( & todo [todo_end ].source , type , name , path , id ) ;
103
+ todo [todo_end ].source = * gs ;
105
104
if (opt -> binary != GREP_BINARY_TEXT )
106
105
grep_source_load_driver (& todo [todo_end ].source );
107
106
todo [todo_end ].done = 0 ;
@@ -317,6 +316,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
317
316
const char * path )
318
317
{
319
318
struct strbuf pathbuf = STRBUF_INIT ;
319
+ struct grep_source gs ;
320
320
321
321
if (opt -> relative && opt -> prefix_length ) {
322
322
quote_path_relative (filename + tree_name_len , opt -> prefix , & pathbuf );
@@ -325,19 +325,22 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
325
325
strbuf_addstr (& pathbuf , filename );
326
326
}
327
327
328
+ grep_source_init (& gs , GREP_SOURCE_OID , pathbuf .buf , path , oid );
329
+ strbuf_release (& pathbuf );
330
+
328
331
#ifndef NO_PTHREADS
329
332
if (num_threads ) {
330
- add_work (opt , GREP_SOURCE_OID , pathbuf .buf , path , oid );
331
- strbuf_release (& pathbuf );
333
+ /*
334
+ * add_work() copies gs and thus assumes ownership of
335
+ * its fields, so do not call grep_source_clear()
336
+ */
337
+ add_work (opt , & gs );
332
338
return 0 ;
333
339
} else
334
340
#endif
335
341
{
336
- struct grep_source gs ;
337
342
int hit ;
338
343
339
- grep_source_init (& gs , GREP_SOURCE_OID , pathbuf .buf , path , oid );
340
- strbuf_release (& pathbuf );
341
344
hit = grep_source (opt , & gs );
342
345
343
346
grep_source_clear (& gs );
@@ -348,25 +351,29 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
348
351
static int grep_file (struct grep_opt * opt , const char * filename )
349
352
{
350
353
struct strbuf buf = STRBUF_INIT ;
354
+ struct grep_source gs ;
351
355
352
356
if (opt -> relative && opt -> prefix_length )
353
357
quote_path_relative (filename , opt -> prefix , & buf );
354
358
else
355
359
strbuf_addstr (& buf , filename );
356
360
361
+ grep_source_init (& gs , GREP_SOURCE_FILE , buf .buf , filename , filename );
362
+ strbuf_release (& buf );
363
+
357
364
#ifndef NO_PTHREADS
358
365
if (num_threads ) {
359
- add_work (opt , GREP_SOURCE_FILE , buf .buf , filename , filename );
360
- strbuf_release (& buf );
366
+ /*
367
+ * add_work() copies gs and thus assumes ownership of
368
+ * its fields, so do not call grep_source_clear()
369
+ */
370
+ add_work (opt , & gs );
361
371
return 0 ;
362
372
} else
363
373
#endif
364
374
{
365
- struct grep_source gs ;
366
375
int hit ;
367
376
368
- grep_source_init (& gs , GREP_SOURCE_FILE , buf .buf , filename , filename );
369
- strbuf_release (& buf );
370
377
hit = grep_source (opt , & gs );
371
378
372
379
grep_source_clear (& gs );
0 commit comments