src/libFLAC/stream_decoder.c : Fix buffer read overflow.
[platform/upstream/flac.git] / src / metaflac / options.c
index 9c9e591..a9629b2 100644 (file)
@@ -1,5 +1,6 @@
 /* metaflac - Command-line FLAC metadata editor
- * Copyright (C) 2001,2002,2003,2004,2005,2006  Josh Coalson
+ * Copyright (C) 2001-2009  Josh Coalson
+ * Copyright (C) 2011-2013  Xiph.Org Foundation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -11,9 +12,9 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #if HAVE_CONFIG_H
@@ -24,6 +25,8 @@
 #include "usage.h"
 #include "utils.h"
 #include "FLAC/assert.h"
+#include "share/alloc.h"
+#include "share/compat.h"
 #include "share/grabbag/replaygain.h"
 #include <ctype.h>
 #include <stdio.h>
@@ -61,15 +64,15 @@ struct share__option long_options_[] = {
        { "set-channels", 1, 0, 0 }, /* undocumented */
        { "set-bps", 1, 0, 0 }, /* undocumented */
        { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
-       { "show-vendor-tag", 0, 0, 0 }, 
-       { "show-tag", 1, 0, 0 }, 
-       { "remove-all-tags", 0, 0, 0 }, 
-       { "remove-tag", 1, 0, 0 }, 
-       { "remove-first-tag", 1, 0, 0 }, 
-       { "set-tag", 1, 0, 0 }, 
-       { "set-tag-from-file", 1, 0, 0 }, 
-       { "import-tags-from", 1, 0, 0 }, 
-       { "export-tags-to", 1, 0, 0 }, 
+       { "show-vendor-tag", 0, 0, 0 },
+       { "show-tag", 1, 0, 0 },
+       { "remove-all-tags", 0, 0, 0 },
+       { "remove-tag", 1, 0, 0 },
+       { "remove-first-tag", 1, 0, 0 },
+       { "set-tag", 1, 0, 0 },
+       { "set-tag-from-file", 1, 0, 0 },
+       { "import-tags-from", 1, 0, 0 },
+       { "export-tags-to", 1, 0, 0 },
        { "import-cuesheet-from", 1, 0, 0 },
        { "export-cuesheet-to", 1, 0, 0 },
        { "import-picture-from", 1, 0, 0 },
@@ -176,8 +179,8 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
                options->prefix_with_filename = (argc - share__optind > 1);
 
        if(share__optind >= argc && !options->show_long_help && !options->show_version) {
-               fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
-               fprintf(stderr,"       metaflac cannot be used as a pipe\n");
+               flac_fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
+               flac_fprintf(stderr,"       metaflac cannot be used as a pipe\n");
                had_error = true;
        }
 
@@ -185,7 +188,7 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
 
        if(options->num_files > 0) {
                unsigned i = 0;
-               if(0 == (options->filenames = (char**)malloc(sizeof(char*) * options->num_files)))
+               if(0 == (options->filenames = safe_malloc_mul_2op_(sizeof(char*), /*times*/options->num_files)))
                        die("out of memory allocating space for file names list");
                while(share__optind < argc)
                        options->filenames[i++] = local_strdup(argv[share__optind++]);
@@ -193,11 +196,11 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
 
        if(options->args.checks.num_major_ops > 0) {
                if(options->args.checks.num_major_ops > 1) {
-                       fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
+                       flac_fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
                        had_error = true;
                }
                else if(options->args.checks.num_shorthand_ops > 0) {
-                       fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
+                       flac_fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
                        had_error = true;
                }
        }
@@ -205,28 +208,28 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
        /* check for only one FLAC file used with certain options */
        if(options->num_files > 1) {
                if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
-                       fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from'\n");
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from'\n");
                        had_error = true;
                }
                if(0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) {
-                       fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-cuesheet-to'\n");
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-cuesheet-to'\n");
                        had_error = true;
                }
                if(0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)) {
-                       fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-picture-to'\n");
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-picture-to'\n");
                        had_error = true;
                }
                if(
                        0 != find_shorthand_operation(options, OP__IMPORT_VC_FROM) &&
                        0 == strcmp(find_shorthand_operation(options, OP__IMPORT_VC_FROM)->argument.filename.value, "-")
                ) {
-                       fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-tags-from=-'\n");
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-tags-from=-'\n");
                        had_error = true;
                }
        }
 
        if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
-               fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
+               flac_fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
                had_error = true;
        }
 
@@ -235,7 +238,7 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
 
        /*
         * We need to create an OP__ADD_SEEKPOINT operation if there is
-        * not one already,  and --import-cuesheet-from was specified but
+        * not one already, and --import-cuesheet-from was specified but
         * --no-cued-seekpoints was not:
         */
        if(options->cued_seekpoints) {
@@ -248,7 +251,7 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
                }
        }
 
-       return !had_error;
+       return had_error;
 }
 
 void free_options(CommandLineOptions *options)
@@ -398,7 +401,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op = append_shorthand_operation(options, OP__SET_MD5SUM);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
-                       fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
                        ok = false;
                }
                else
@@ -407,7 +410,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-min-blocksize")) {
                op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
-                       fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
                        ok = false;
                }
                else
@@ -416,7 +419,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-max-blocksize")) {
                op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
-                       fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
                        ok = false;
                }
                else
@@ -425,7 +428,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-min-framesize")) {
                op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
-                       fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
                        ok = false;
                }
                else
@@ -434,7 +437,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-max-framesize")) {
                op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
-                       fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
                        ok = false;
                }
                else
@@ -443,7 +446,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-sample-rate")) {
                op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
-                       fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
                        ok = false;
                }
                else
@@ -452,7 +455,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-channels")) {
                op = append_shorthand_operation(options, OP__SET_CHANNELS);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
-                       fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
                        ok = false;
                }
                else
@@ -461,7 +464,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-bps")) {
                op = append_shorthand_operation(options, OP__SET_BPS);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BITS_PER_SAMPLE || op->argument.streaminfo_uint32.value > FLAC__MAX_BITS_PER_SAMPLE) {
-                       fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
                        ok = false;
                }
                else
@@ -469,8 +472,8 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        }
        else if(0 == strcmp(opt, "set-total-samples")) {
                op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
-               if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
-                       fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
+               if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (((FLAC__uint64)1)<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
+                       flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
                        ok = false;
                }
                else
@@ -485,7 +488,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                FLAC__ASSERT(0 != option_argument);
                if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
@@ -498,7 +501,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                FLAC__ASSERT(0 != option_argument);
                if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
@@ -508,7 +511,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                FLAC__ASSERT(0 != option_argument);
                if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
@@ -519,7 +522,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op->argument.vc_field.field_value_from_file = false;
                if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
@@ -530,7 +533,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op->argument.vc_field.field_value_from_file = true;
                if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
@@ -538,7 +541,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_string(option_argument, &(op->argument.filename.value))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
        }
@@ -546,19 +549,19 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_string(option_argument, &(op->argument.filename.value))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
        }
        else if(0 == strcmp(opt, "import-cuesheet-from")) {
                if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
-                       fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
                        ok = false;
                }
                op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
        }
@@ -566,7 +569,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_string(option_argument, &(op->argument.filename.value))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
        }
@@ -574,16 +577,16 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op = append_shorthand_operation(options, OP__IMPORT_PICTURE_FROM);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_string(option_argument, &(op->argument.specification.value))) {
-                       fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
                        ok = false;
                }
        }
        else if(0 == strcmp(opt, "export-picture-to")) {
-               const Argument *arg = find_argument(options, ARG__BLOCK_NUMBER);
+               arg = find_argument(options, ARG__BLOCK_NUMBER);
                op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
                op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
@@ -594,7 +597,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                FLAC__ASSERT(0 != option_argument);
                if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
                else {
@@ -627,7 +630,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op = append_shorthand_operation(options, OP__ADD_PADDING);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
-                       fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
+                       flac_fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
                        ok = false;
                }
        }
@@ -659,7 +662,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__BLOCK_NUMBER);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_block_number(option_argument, &(arg->value.block_number))) {
-                       fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
+                       flac_fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
                        ok = false;
                }
        }
@@ -667,7 +670,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__BLOCK_TYPE);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_block_type(option_argument, &(arg->value.block_type))) {
-                       fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
                        ok = false;
                }
                options->args.checks.has_block_type = true;
@@ -676,7 +679,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_block_type(option_argument, &(arg->value.block_type))) {
-                       fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
                        ok = false;
                }
                options->args.checks.has_except_block_type = true;
@@ -685,14 +688,14 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__DATA_FORMAT);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_data_format(option_argument, &(arg->value.data_format))) {
-                       fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
                        ok = false;
                }
        }
        else if(0 == strcmp(opt, "application-data-format")) {
                FLAC__ASSERT(0 != option_argument);
                if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
-                       fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
                        ok = false;
                }
        }
@@ -712,14 +715,16 @@ void append_new_operation(CommandLineOptions *options, Operation operation)
 {
        if(options->ops.capacity == 0) {
                options->ops.capacity = 50;
-               if(0 == (options->ops.operations = (Operation*)malloc(sizeof(Operation) * options->ops.capacity)))
+               if(0 == (options->ops.operations = malloc(sizeof(Operation) * options->ops.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
        }
        if(options->ops.capacity <= options->ops.num_operations) {
                unsigned original_capacity = options->ops.capacity;
-               options->ops.capacity *= 4;
-               if(0 == (options->ops.operations = (Operation*)realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
+               if(options->ops.capacity > UINT32_MAX / 2) /* overflow check */
+                       die("out of memory allocating space for option list");
+               options->ops.capacity *= 2;
+               if(0 == (options->ops.operations = safe_realloc_mul_2op_(options->ops.operations, sizeof(Operation), /*times*/options->ops.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
        }
@@ -731,14 +736,16 @@ void append_new_argument(CommandLineOptions *options, Argument argument)
 {
        if(options->args.capacity == 0) {
                options->args.capacity = 50;
-               if(0 == (options->args.arguments = (Argument*)malloc(sizeof(Argument) * options->args.capacity)))
+               if(0 == (options->args.arguments = malloc(sizeof(Argument) * options->args.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
        }
        if(options->args.capacity <= options->args.num_arguments) {
                unsigned original_capacity = options->args.capacity;
-               options->args.capacity *= 4;
-               if(0 == (options->args.arguments = (Argument*)realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
+               if(options->args.capacity > UINT32_MAX / 2) /* overflow check */
+                       die("out of memory allocating space for option list");
+               options->args.capacity *= 2;
+               if(0 == (options->args.arguments = safe_realloc_mul_2op_(options->args.arguments, sizeof(Argument), /*times*/options->args.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
        }
@@ -835,34 +842,12 @@ FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
        return true;
 }
 
-#ifdef _MSC_VER
-/* There's no strtoull() in MSVC6 so we just write a specialized one */
-static FLAC__uint64 local__strtoull(const char *src)
-{
-       FLAC__uint64 ret = 0;
-       int c;
-       FLAC__ASSERT(0 != src);
-       while(0 != (c = *src++)) {
-               c -= '0';
-               if(c >= 0 && c <= 9)
-                       ret = (ret * 10) + c;
-               else
-                       break;
-       }
-       return ret;
-}
-#endif
-
 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
 {
        FLAC__ASSERT(0 != src);
        if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
                return false;
-#ifdef _MSC_VER
-       *dest = local__strtoull(src);
-#else
        *dest = strtoull(src, 0, 10);
-#endif
        return true;
 }
 
@@ -969,7 +954,7 @@ FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
 
        /* make space */
        FLAC__ASSERT(out->num_entries > 0);
-       if(0 == (out->entries = (unsigned*)malloc(sizeof(unsigned) * out->num_entries)))
+       if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(unsigned), /*times*/out->num_entries)))
                die("out of memory allocating space for option list");
 
        /* load 'em up */
@@ -1008,7 +993,7 @@ FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
 
        /* make space */
        FLAC__ASSERT(out->num_entries > 0);
-       if(0 == (out->entries = (Argument_BlockTypeEntry*)malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
+       if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(Argument_BlockTypeEntry), /*times*/out->num_entries)))
                die("out of memory allocating space for option list");
 
        /* load 'em up */
@@ -1035,8 +1020,8 @@ FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
                        out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
                        out->entries[entry].filter_application_by_id = (0 != r);
                        if(0 != r) {
-                               if(strlen(r) == 4) {
-                                       strcpy(out->entries[entry].application_id, r);
+                               if(strlen(r) == sizeof (out->entries[entry].application_id)) {
+                                       memcpy(out->entries[entry].application_id, r, sizeof (out->entries[entry].application_id));
                                }
                                else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
                                        FLAC__uint32 x = strtoul(r+2, 0, 16);
@@ -1100,5 +1085,5 @@ FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
 
 void undocumented_warning(const char *opt)
 {
-       fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n         only for repairing a damaged STREAMINFO block\n", opt);
+       flac_fprintf(stderr, "WARNING: undocumented option --%s should be used with caution,\n         only for repairing a damaged STREAMINFO block\n", opt);
 }