hvif2png: Fix closing of streams

* h2p_close_state():
  - Don't close stdin/stdout.
  - Set in/out to NULL to prevent accidental double closing.
* h2p_open_streams(): Simplify closing on error. No need for
  fclose_quietly().
This commit is contained in:
Ingo Weinhold 2015-07-18 09:04:33 +02:00
parent afdc8efb27
commit 0721444745

View File

@ -68,17 +68,17 @@ h2p_close_state(h2p_state* state)
if (state->hvif_buffer.buffer != NULL) if (state->hvif_buffer.buffer != NULL)
free(state->hvif_buffer.buffer); free(state->hvif_buffer.buffer);
if (state->in != NULL) if (state->in != NULL) {
if (state->in != stdin)
fclose(state->in); fclose(state->in);
state->in = NULL;
if (state->out != NULL)
fclose(state->out);
} }
if (state->out != NULL) {
static void fclose_quietly(FILE** f) if (state->out != stdout)
{ fclose(state->out);
fclose(*f); state->out = NULL;
}
} }
@ -89,6 +89,8 @@ static void fclose_quietly(FILE** f)
static bool static bool
h2p_open_streams(h2p_state* state) h2p_open_streams(h2p_state* state)
{ {
CObjectDeleter<h2p_state> stateCloser(state, &h2p_close_state);
if (state->params.in_filename != NULL) if (state->params.in_filename != NULL)
state->in = fopen(state->params.in_filename, "rb"); state->in = fopen(state->params.in_filename, "rb");
else else
@ -100,8 +102,6 @@ h2p_open_streams(h2p_state* state)
return false; return false;
} }
CObjectDeleter<FILE*> inDeleter(&state->in, fclose_quietly);
if (state->params.out_filename != NULL) if (state->params.out_filename != NULL)
state->out = fopen(state->params.out_filename, "wb"); state->out = fopen(state->params.out_filename, "wb");
else else
@ -113,7 +113,7 @@ h2p_open_streams(h2p_state* state)
return false; return false;
} }
inDeleter.Detach(); stateCloser.Detach();
return true; return true;
} }