flacdec_close(): Be liberal in what you accept.
authorAndre Noll <maan@systemlinux.org>
Tue, 10 Apr 2012 05:38:20 +0000 (07:38 +0200)
committerAndre Noll <maan@systemlinux.org>
Sun, 3 Jun 2012 11:04:01 +0000 (13:04 +0200)
Functions like close() which terminate an instance of some subsystem
should always be idempotent, i.e. when calling such a function twice
with the same argument, the second call should be a noop.

However, flacdec_close() violates this rule because it dereferences
its private pointer unconditionally. This patch makes the function
idempotent.

flacdec_filter.c

index 40246f2..cd086f4 100644 (file)
@@ -264,10 +264,17 @@ out:
 
 static void flacdec_close(struct filter_node *fn)
 {
-       struct private_flacdec_data *pfd = fn->private_data;
+       struct private_flacdec_data *pfd;
 
-       FLAC__stream_decoder_finish(pfd->decoder);
-       FLAC__stream_decoder_delete(pfd->decoder);
+       if (!fn)
+               return;
+       pfd = fn->private_data;
+       if (!pfd)
+               return;
+       if (pfd->decoder) {
+               FLAC__stream_decoder_finish(pfd->decoder);
+               FLAC__stream_decoder_delete(pfd->decoder);
+       }
        free(pfd);
        fn->private_data = NULL;
 }