@@ -556,7 +556,6 @@ static void kill_multi_file_filter(struct hashmap *hashmap, struct subprocess_en
556556 finish_command (& entry -> process );
557557
558558 hashmap_remove (hashmap , entry , NULL );
559- free (entry );
560559}
561560
562561static void stop_multi_file_filter (struct child_process * process )
@@ -570,14 +569,15 @@ static void stop_multi_file_filter(struct child_process *process)
570569 finish_command (process );
571570}
572571
573- static int start_multi_file_filter_fn (struct cmd2process * entry )
572+ static int start_multi_file_filter_fn (struct subprocess_entry * subprocess )
574573{
575574 int err ;
575+ struct cmd2process * entry = (struct cmd2process * )subprocess ;
576576 struct string_list cap_list = STRING_LIST_INIT_NODUP ;
577577 char * cap_buf ;
578578 const char * cap_name ;
579- struct child_process * process = & entry -> subprocess . process ;
580- const char * cmd = entry -> subprocess . cmd ;
579+ struct child_process * process = & subprocess -> process ;
580+ const char * cmd = subprocess -> cmd ;
581581
582582 sigchain_push (SIGPIPE , SIG_IGN );
583583
@@ -629,17 +629,16 @@ static int start_multi_file_filter_fn(struct cmd2process *entry)
629629 return err ;
630630}
631631
632- static struct cmd2process * start_multi_file_filter (struct hashmap * hashmap , const char * cmd )
632+ typedef int (* subprocess_start_fn )(struct subprocess_entry * entry );
633+ int start_multi_file_filter (struct hashmap * hashmap , struct subprocess_entry * entry , const char * cmd ,
634+ subprocess_start_fn startfn )
633635{
634636 int err ;
635- struct cmd2process * entry ;
636637 struct child_process * process ;
637638 const char * argv [] = { cmd , NULL };
638639
639- entry = xmalloc (sizeof (* entry ));
640- entry -> subprocess .cmd = cmd ;
641- entry -> supported_capabilities = 0 ;
642- process = & entry -> subprocess .process ;
640+ entry -> cmd = cmd ;
641+ process = & entry -> process ;
643642
644643 child_process_init (process );
645644 process -> argv = argv ;
@@ -649,22 +648,23 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons
649648 process -> clean_on_exit = 1 ;
650649 process -> clean_on_exit_handler = stop_multi_file_filter ;
651650
652- if (start_command (process )) {
651+ err = start_command (process );
652+ if (err ) {
653653 error ("cannot fork to run external filter '%s'" , cmd );
654- return NULL ;
654+ return err ;
655655 }
656656
657657 hashmap_entry_init (entry , strhash (cmd ));
658658
659- err = start_multi_file_filter_fn (entry );
659+ err = startfn (entry );
660660 if (err ) {
661661 error ("initialization for external filter '%s' failed" , cmd );
662- kill_multi_file_filter (hashmap , & entry -> subprocess );
663- return NULL ;
662+ kill_multi_file_filter (hashmap , entry );
663+ return err ;
664664 }
665665
666666 hashmap_add (hashmap , entry );
667- return entry ;
667+ return 0 ;
668668}
669669
670670static int apply_multi_file_filter (const char * path , const char * src , size_t len ,
@@ -689,9 +689,13 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
689689 fflush (NULL );
690690
691691 if (!entry ) {
692- entry = start_multi_file_filter (& cmd_process_map , cmd );
693- if (!entry )
692+ entry = xmalloc (sizeof (* entry ));
693+ entry -> supported_capabilities = 0 ;
694+
695+ if (start_multi_file_filter (& cmd_process_map , & entry -> subprocess , cmd , start_multi_file_filter_fn )) {
696+ free (entry );
694697 return 0 ;
698+ }
695699 }
696700 process = & entry -> subprocess .process ;
697701
@@ -765,6 +769,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
765769 */
766770 error ("external filter '%s' failed" , cmd );
767771 kill_multi_file_filter (& cmd_process_map , & entry -> subprocess );
772+ free (entry );
768773 }
769774 } else {
770775 strbuf_swap (dst , & nbuf );
0 commit comments