From 3c84f9e86b40b64bb42af6dff8c61521c5439ecd Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Sun, 17 Mar 2013 20:56:41 +1100 Subject: [PATCH] Use new function flac_snprintf() where ever appropriate. This replaces un-safe usage of sprintf() and Micorsoft's _snprintf() with something sane. --- src/flac/analyze.c | 2 +- src/flac/utils.c | 13 +++++-------- src/metaflac/operations_shorthand_cuesheet.c | 9 ++------- src/monkeys_audio_utilities/flac_mac/main.c | 10 +++++----- src/plugin_xmms/http.c | 4 ++-- src/plugin_xmms/plugin.c | 5 +++-- src/share/grabbag/replaygain.c | 6 +----- src/test_grabbag/cuesheet/main.c | 4 ++-- src/test_grabbag/picture/main.c | 12 ++---------- src/test_streams/Makefile.am | 2 +- src/test_streams/main.c | 11 ++++++----- 11 files changed, 30 insertions(+), 48 deletions(-) diff --git a/src/flac/analyze.c b/src/flac/analyze.c index 51522932..f5a3f2e4 100644 --- a/src/flac/analyze.c +++ b/src/flac/analyze.c @@ -152,7 +152,7 @@ void flac__analyze_frame(const FLAC__Frame *frame, unsigned frame_number, FLAC__ } /* write the subframe */ - sprintf(outfilename, "f%06u.s%u.gp", frame_number, channel); + flac_snprintf(outfilename, sizeof (outfilename), "f%06u.s%u.gp", frame_number, channel); compute_stats(&stats); (void)dump_stats(&stats, outfilename); diff --git a/src/flac/utils.c b/src/flac/utils.c index 0f1a3018..4b36141b 100644 --- a/src/flac/utils.c +++ b/src/flac/utils.c @@ -20,14 +20,15 @@ # include #endif -#include "utils.h" -#include "FLAC/assert.h" -#include "FLAC/metadata.h" #include #include #include #include #include +#include "utils.h" +#include "FLAC/assert.h" +#include "FLAC/metadata.h" +#include "share/compat.h" const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK"; @@ -285,11 +286,7 @@ FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__ FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); FLAC__ASSERT(strlen(CHANNEL_MASK_TAG+1+2+16+1) <= sizeof(tag)); /* +1 for =, +2 for 0x, +16 for digits, +1 for NUL */ entry.entry = (FLAC__byte*)tag; -#if defined _MSC_VER || defined __MINGW32__ - if((entry.length = _snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag)) -#else - if((entry.length = snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag)) -#endif + if((entry.length = flac_snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag)) return false; if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true)) return false; diff --git a/src/metaflac/operations_shorthand_cuesheet.c b/src/metaflac/operations_shorthand_cuesheet.c index d59c1afd..f59bc899 100644 --- a/src/metaflac/operations_shorthand_cuesheet.c +++ b/src/metaflac/operations_shorthand_cuesheet.c @@ -21,7 +21,6 @@ #endif #include -#include /* for snprintf() */ #include #include "options.h" #include "utils.h" @@ -163,7 +162,7 @@ FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, for(track = 0; track < cs->num_tracks; track++) { const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track; for(index = 0; index < tr->num_indices; index++) { - sprintf(spec, "%" PRIu64 ";", (tr->offset + tr->indices[index].offset)); + flac_snprintf(spec, sizeof (spec), "%" PRIu64 ";", (tr->offset + tr->indices[index].offset)); local_strcat(seekpoint_specification, spec); } } @@ -201,11 +200,7 @@ FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cueshe return false; } -#if defined _MSC_VER || defined __MINGW32__ - _snprintf(ref, reflen, "\"%s\" FLAC", filename); -#else - snprintf(ref, reflen, "\"%s\" FLAC", filename); -#endif + flac_snprintf(ref, reflen, "\"%s\" FLAC", filename); grabbag__cuesheet_emit(f, cuesheet, ref); diff --git a/src/monkeys_audio_utilities/flac_mac/main.c b/src/monkeys_audio_utilities/flac_mac/main.c index 0f177874..21d4f715 100644 --- a/src/monkeys_audio_utilities/flac_mac/main.c +++ b/src/monkeys_audio_utilities/flac_mac/main.c @@ -100,14 +100,14 @@ int main(int argc, char *argv[]) return -4; /* build the command to call flac with */ - sprintf(prog, "%sflac.exe", macdir); - sprintf(options, "-%d", flac_level); + flac_snprintf(prog, sizeof (prog), "%sflac.exe", macdir); + flac_snprintf(options, sizeof (options), "-%d", flac_level); for(i = opt_arg; i < argc; i++) if(argv[i][0] == '-') { strcat(options, " "); strcat(options, argv[i]); } - sprintf(cmdline, "\"%s\" %s -o \"%s\" \"%s\"", prog, options, argv[to_arg], argv[from_arg]); + flac_snprintf(cmdline, sizeof (cmdline), "\"%s\" %s -o \"%s\" \"%s\"", prog, options, argv[to_arg], argv[from_arg]); flac_return_val = execit(prog, cmdline); @@ -139,8 +139,8 @@ int main(int argc, char *argv[]) } strcpy(strrchr(to,'.'), ".flac"); - sprintf(prog, "%sflac_ren.exe", macdir); - sprintf(cmdline, "\"%s\" \"%s\" \"%s\"", prog, from, to); + flac_snprintf(prog, sizeof (prog), "%sflac_ren.exe", macdir); + flac_snprintf(cmdline, sizeof (smdline), "\"%s\" \"%s\" \"%s\"", prog, from, to); flac_return_val = forkit(prog, cmdline); } diff --git a/src/plugin_xmms/http.c b/src/plugin_xmms/http.c index ce064471..c51f1b83 100644 --- a/src/plugin_xmms/http.c +++ b/src/plugin_xmms/http.c @@ -454,7 +454,7 @@ static int http_connect (gchar *url_, gboolean head, guint64 offset) { udp_port = udp_establish_listener (&udp_sock); if (udp_port > 0) - sprintf (udpspace, "x-audiocast-udpport: %d\r\n", udp_port); + flac_snprintf (udpspace, sizeof (udpspace), "x-audiocast-udpport: %d\r\n", udp_port); else udp_sock = 0; } @@ -879,7 +879,7 @@ static int udp_check_for_data(int sock) else if (strstr(lines[i], "x-audiocast-udpseqnr:") != NULL) { gchar obuf[60]; - sprintf(obuf, "x-audiocast-ack: %ld \r\n", atol(valptr)); + flac_snprintf(obuf, sizeof (obuf), "x-audiocast-ack: %ld \r\n", atol(valptr)); if (sendto(sock, obuf, strlen(obuf), 0, (struct sockaddr *) &from, fromlen) < 0) { g_log(NULL, G_LOG_LEVEL_WARNING, diff --git a/src/plugin_xmms/plugin.c b/src/plugin_xmms/plugin.c index 85147cf0..0346d30a 100644 --- a/src/plugin_xmms/plugin.c +++ b/src/plugin_xmms/plugin.c @@ -402,8 +402,9 @@ void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec) *title = NULL; } else { - *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1); - sprintf(*title, "%s\"%s\"", errtitle, filename); + size_t len = strlen(errtitle) + 1 + strlen(filename) + 1 + 1; + *title = g_malloc(len); + flac_snprintf(*title, len, "%s\"%s\"", errtitle, filename); } } else { *title = NULL; diff --git a/src/share/grabbag/replaygain.c b/src/share/grabbag/replaygain.c index bda3f4cf..88328880 100644 --- a/src/share/grabbag/replaygain.c +++ b/src/share/grabbag/replaygain.c @@ -102,11 +102,7 @@ static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, c if (0 == saved_locale) return false; setlocale(LC_ALL, "C"); -#if defined _MSC_VER || defined __MINGW32__ - _snprintf(buffer, sizeof(buffer)-1, format, name, value); -#else - snprintf(buffer, sizeof(buffer)-1, format, name, value); -#endif + flac_snprintf(buffer, sizeof(buffer), format, name, value); setlocale(LC_ALL, saved_locale); free(saved_locale); diff --git a/src/test_grabbag/cuesheet/main.c b/src/test_grabbag/cuesheet/main.c index 02490611..d04fa4ba 100644 --- a/src/test_grabbag/cuesheet/main.c +++ b/src/test_grabbag/cuesheet/main.c @@ -63,7 +63,7 @@ static int do_cuesheet(const char *infilename, unsigned sample_rate, FLAC__bool FLAC__metadata_object_delete(cuesheet); return 1; } - sprintf(tmpfilename, "%s.1", infilename); + flac_snprintf(tmpfilename, sizeof (tmpfilename), "%s.1", infilename); if(0 == (fout = fopen(tmpfilename, "w"))) { fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno)); FLAC__metadata_object_delete(cuesheet); @@ -95,7 +95,7 @@ static int do_cuesheet(const char *infilename, unsigned sample_rate, FLAC__bool FLAC__metadata_object_delete(cuesheet); return 1; } - sprintf(tmpfilename, "%s.2", infilename); + flac_snprintf(tmpfilename, sizeof (tmpfilename), "%s.2", infilename); if(0 == (fout = fopen(tmpfilename, "w"))) { fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno)); FLAC__metadata_object_delete(cuesheet); diff --git a/src/test_grabbag/picture/main.c b/src/test_grabbag/picture/main.c index 585607a7..766db7d9 100644 --- a/src/test_grabbag/picture/main.c +++ b/src/test_grabbag/picture/main.c @@ -70,17 +70,9 @@ static FLAC__bool test_one_picture(const char *prefix, const PictureFile *pf, co const char *error; char s[4096]; if(fn_only) -#if defined _MSC_VER || defined __MINGW32__ - _snprintf(s, sizeof(s)-1, "%s/%s", prefix, pf->path); -#else - snprintf(s, sizeof(s)-1, "%s/%s", prefix, pf->path); -#endif + flac_snprintf(s, sizeof(s), "%s/%s", prefix, pf->path); else -#if defined _MSC_VER || defined __MINGW32__ - _snprintf(s, sizeof(s)-1, "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path); -#else - snprintf(s, sizeof(s)-1, "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path); -#endif + flac_snprintf(s, sizeof(s), "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path); printf("testing grabbag__picture_parse_specification(\"%s\")... ", s); if(0 == (obj = grabbag__picture_parse_specification(s, &error))) diff --git a/src/test_streams/Makefile.am b/src/test_streams/Makefile.am index 801be9bc..5bda8f8c 100644 --- a/src/test_streams/Makefile.am +++ b/src/test_streams/Makefile.am @@ -24,4 +24,4 @@ noinst_PROGRAMS = test_streams test_streams_SOURCES = \ main.c -test_streams_LDADD = -lm +test_streams_LDADD = $(top_builddir)/src/share/grabbag/libgrabbag.la -lm diff --git a/src/test_streams/main.c b/src/test_streams/main.c index 4e68f0d6..723269bd 100644 --- a/src/test_streams/main.c +++ b/src/test_streams/main.c @@ -31,6 +31,7 @@ #endif #include "FLAC/assert.h" #include "FLAC/ordinals.h" +#include "share/compat.h" #ifndef M_PI /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */ @@ -1136,24 +1137,24 @@ int main(int argc, char *argv[]) for(samples = 0; samples < sizeof(nsamples)/sizeof(nsamples[0]); samples++) { char fn[64]; - sprintf(fn, "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]); + flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]); if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples])) return 1; - sprintf(fn, "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]); + flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]); if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/0)) return 1; - sprintf(fn, "rt-%u-%u-%u.rf64", channels, bits_per_sample, nsamples[samples]); + flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.rf64", channels, bits_per_sample, nsamples[samples]); if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/1)) return 1; - sprintf(fn, "rt-%u-%u-%u.w64", channels, bits_per_sample, nsamples[samples]); + flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.w64", channels, bits_per_sample, nsamples[samples]); if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/2)) return 1; if(bits_per_sample % 8 == 0) { - sprintf(fn, "rt-%u-%u-%u.raw", channels, bits_per_sample, nsamples[samples]); + flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.raw", channels, bits_per_sample, nsamples[samples]); if(!generate_raw(fn, channels, bits_per_sample/8, nsamples[samples])) return 1; } -- 2.11.4.GIT