From a9557c13fa78d73bee0ef44a08057bff6d88acee Mon Sep 17 00:00:00 2001 From: Andrey Astafyev Date: Tue, 19 Nov 2019 05:38:00 -0800 Subject: examples: Fix error condition processing Don't attempt to attach failed metadata objects. This also avoids clobbering the error flag if attaching the objects should succeed. There may still be problems later, for example calling _delete() on a possibly null metadata object pointer, but this it at least and improvement in the compromise between error handling and readability in C. Signed-off-by: Ralph Giles --- examples/c/encode/file/main.c | 8 +++++--- examples/cpp/encode/file/main.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/c/encode/file/main.c b/examples/c/encode/file/main.c index b12d32b1..1066dc37 100644 --- a/examples/c/encode/file/main.c +++ b/examples/c/encode/file/main.c @@ -109,11 +109,13 @@ int main(int argc, char *argv[]) ) { fprintf(stderr, "ERROR: out of memory or tag error\n"); ok = false; - } + } else { + + metadata[1]->length = 1234; /* set the padding length */ - metadata[1]->length = 1234; /* set the padding length */ + ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2); - ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2); + } } /* initialize encoder */ diff --git a/examples/cpp/encode/file/main.cpp b/examples/cpp/encode/file/main.cpp index 7d44b873..c66922c4 100644 --- a/examples/cpp/encode/file/main.cpp +++ b/examples/cpp/encode/file/main.cpp @@ -117,11 +117,13 @@ int main(int argc, char *argv[]) ) { fprintf(stderr, "ERROR: out of memory or tag error\n"); ok = false; - } + } else { + + metadata[1]->length = 1234; /* set the padding length */ - metadata[1]->length = 1234; /* set the padding length */ + ok = encoder.set_metadata(metadata, 2); - ok = encoder.set_metadata(metadata, 2); + } } /* initialize encoder */ -- cgit v1.2.1