@@ -2963,7 +2963,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
29632963
29642964 if (err ) {
29652965 free (ieot );
2966- return err ;
2966+ goto cleanup ;
29672967 }
29682968
29692969 offset = hashfile_total (f );
@@ -2992,8 +2992,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
29922992 hashwrite (f , sb .buf , sb .len );
29932993 strbuf_release (& sb );
29942994 free (ieot );
2995- if (err )
2996- return -1 ;
2995+ /*
2996+ * NEEDSWORK: write_index_ext_header() never returns a failure,
2997+ * and this part may want to be simplified.
2998+ */
2999+ if (err ) {
3000+ err = -1 ;
3001+ goto cleanup ;
3002+ }
29973003 }
29983004
29993005 if (write_extensions & WRITE_SPLIT_INDEX_EXTENSION &&
@@ -3008,8 +3014,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30083014 sb .len ) < 0 ;
30093015 hashwrite (f , sb .buf , sb .len );
30103016 strbuf_release (& sb );
3011- if (err )
3012- return -1 ;
3017+ /*
3018+ * NEEDSWORK: write_link_extension() never returns a failure,
3019+ * and this part may want to be simplified.
3020+ */
3021+ if (err ) {
3022+ err = -1 ;
3023+ goto cleanup ;
3024+ }
30133025 }
30143026 if (write_extensions & WRITE_CACHE_TREE_EXTENSION &&
30153027 !drop_cache_tree && istate -> cache_tree ) {
@@ -3019,8 +3031,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30193031 err = write_index_ext_header (f , eoie_c , CACHE_EXT_TREE , sb .len ) < 0 ;
30203032 hashwrite (f , sb .buf , sb .len );
30213033 strbuf_release (& sb );
3022- if (err )
3023- return -1 ;
3034+ /*
3035+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3036+ * and this part may want to be simplified.
3037+ */
3038+ if (err ) {
3039+ err = -1 ;
3040+ goto cleanup ;
3041+ }
30243042 }
30253043 if (write_extensions & WRITE_RESOLVE_UNDO_EXTENSION &&
30263044 istate -> resolve_undo ) {
@@ -3031,8 +3049,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30313049 sb .len ) < 0 ;
30323050 hashwrite (f , sb .buf , sb .len );
30333051 strbuf_release (& sb );
3034- if (err )
3035- return -1 ;
3052+ /*
3053+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3054+ * and this part may want to be simplified.
3055+ */
3056+ if (err ) {
3057+ err = -1 ;
3058+ goto cleanup ;
3059+ }
30363060 }
30373061 if (write_extensions & WRITE_UNTRACKED_CACHE_EXTENSION &&
30383062 istate -> untracked ) {
@@ -3043,8 +3067,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30433067 sb .len ) < 0 ;
30443068 hashwrite (f , sb .buf , sb .len );
30453069 strbuf_release (& sb );
3046- if (err )
3047- return -1 ;
3070+ /*
3071+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3072+ * and this part may want to be simplified.
3073+ */
3074+ if (err ) {
3075+ err = -1 ;
3076+ goto cleanup ;
3077+ }
30483078 }
30493079 if (write_extensions & WRITE_FSMONITOR_EXTENSION &&
30503080 istate -> fsmonitor_last_update ) {
@@ -3054,12 +3084,25 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30543084 err = write_index_ext_header (f , eoie_c , CACHE_EXT_FSMONITOR , sb .len ) < 0 ;
30553085 hashwrite (f , sb .buf , sb .len );
30563086 strbuf_release (& sb );
3057- if (err )
3058- return -1 ;
3087+ /*
3088+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3089+ * and this part may want to be simplified.
3090+ */
3091+ if (err ) {
3092+ err = -1 ;
3093+ goto cleanup ;
3094+ }
30593095 }
30603096 if (istate -> sparse_index ) {
3061- if (write_index_ext_header (f , eoie_c , CACHE_EXT_SPARSE_DIRECTORIES , 0 ) < 0 )
3062- return -1 ;
3097+ err = write_index_ext_header (f , eoie_c , CACHE_EXT_SPARSE_DIRECTORIES , 0 );
3098+ /*
3099+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3100+ * and this part may want to be simplified.
3101+ */
3102+ if (err ) {
3103+ err = -1 ;
3104+ goto cleanup ;
3105+ }
30633106 }
30643107
30653108 /*
@@ -3075,8 +3118,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30753118 err = write_index_ext_header (f , NULL , CACHE_EXT_ENDOFINDEXENTRIES , sb .len ) < 0 ;
30763119 hashwrite (f , sb .buf , sb .len );
30773120 strbuf_release (& sb );
3078- if (err )
3079- return -1 ;
3121+ /*
3122+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3123+ * and this part may want to be simplified.
3124+ */
3125+ if (err ) {
3126+ err = -1 ;
3127+ goto cleanup ;
3128+ }
30803129 }
30813130
30823131 csum_fsync_flag = 0 ;
@@ -3085,13 +3134,16 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30853134
30863135 finalize_hashfile (f , istate -> oid .hash , FSYNC_COMPONENT_INDEX ,
30873136 CSUM_HASH_IN_STREAM | csum_fsync_flag );
3137+ f = NULL ;
30883138
30893139 if (close_tempfile_gently (tempfile )) {
3090- error (_ ("could not close '%s'" ), get_tempfile_path (tempfile ));
3091- return -1 ;
3140+ err = error (_ ("could not close '%s'" ), get_tempfile_path (tempfile ));
3141+ goto cleanup ;
3142+ }
3143+ if (stat (get_tempfile_path (tempfile ), & st )) {
3144+ err = error_errno (_ ("could not stat '%s'" ), get_tempfile_path (tempfile ));
3145+ goto cleanup ;
30923146 }
3093- if (stat (get_tempfile_path (tempfile ), & st ))
3094- return -1 ;
30953147 istate -> timestamp .sec = (unsigned int )st .st_mtime ;
30963148 istate -> timestamp .nsec = ST_MTIME_NSEC (st );
30973149 trace_performance_since (start , "write index, changed mask = %x" , istate -> cache_changed );
@@ -3106,6 +3158,11 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
31063158 istate -> cache_nr );
31073159
31083160 return 0 ;
3161+
3162+ cleanup :
3163+ if (f )
3164+ discard_hashfile (f );
3165+ return err ;
31093166}
31103167
31113168void set_alternate_index_output (const char * name )
0 commit comments