[PATCH] incorrect return type for opt_bitrate/type safety
Hello, during use I discovered that my -b and -vb options were rejected by ffmpeg. In the `options' array, the `opt_bitrate' function is declared to be a OPT_FUNC2 handler, yet it void. OPT_FUNC2 handlers are supposed to return int, and negative values for failures. This is symptomatic of lack of type safety; the compiler could have warned about passing an incorrect function pointer type if given the chance, but in every case the pointers were casted to (void*). I have attached a patch which establishes type safety in the `options' array, in addition to removing all the warnings this caused. -- Morten Hustveit
On Wed, Jan 02, 2008 at 12:25:22AM +0100, Morten Hustveit wrote:
Hello,
during use I discovered that my -b and -vb options were rejected by ffmpeg. In the `options' array, the `opt_bitrate' function is declared to be a OPT_FUNC2 handler, yet it void. OPT_FUNC2 handlers are supposed to return int, and negative values for failures.
This is symptomatic of lack of type safety; the compiler could have warned about passing an incorrect function pointer type if given the chance, but in every case the pointers were casted to (void*). I have attached a patch which establishes type safety in the `options' array, in addition to removing all the warnings this caused.
-- Morten Hustveit
Index: ffmpeg.c =================================================================== --- ffmpeg.c (revision 11364) +++ ffmpeg.c (working copy) @@ -125,7 +125,7 @@ static float video_rc_qmod_amp=0; static int video_rc_qmod_freq=0; #endif -static char *video_rc_override_string=NULL; +static const char *video_rc_override_string=NULL; static int video_disable = 0; static int video_discard = 0; static char *video_codec_name = NULL;
adding const to stuff should be a seperate patch
@@ -204,7 +204,7 @@ static int nb_frames_dup = 0; static int nb_frames_drop = 0; static int input_sync; -static uint64_t limit_filesize = 0; // +static int64_t limit_filesize = 0; //
filesize is not signed [...]
@@ -2180,7 +2206,7 @@ } }
-static void opt_bitrate(const char *opt, const char *arg) +static int opt_bitrate(const char *opt, const char *arg) { int codec_type = opt[0]=='a' ? CODEC_TYPE_AUDIO : CODEC_TYPE_VIDEO;
@@ -2188,6 +2214,8 @@
if (av_get_int(avctx_opts[codec_type], "b", NULL) < 1000) fprintf(stderr, "WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s\n"); + + return 0; }
fixing the return type should also be a seperate patch [...]
@@ -3092,7 +3112,7 @@ subtitle_stream_copy = 0; }
-static void opt_new_audio_stream(void) +static void opt_new_audio_stream(const char *arg av_unused) { AVFormatContext *oc; if (nb_output_files <= 0) { @@ -3103,7 +3123,7 @@ new_audio_stream(oc); }
-static void opt_new_video_stream(void) +static void opt_new_video_stream(const char *arg av_unused) { AVFormatContext *oc; if (nb_output_files <= 0) { @@ -3114,7 +3134,7 @@ new_video_stream(oc); }
-static void opt_new_subtitle_stream(void) +static void opt_new_subtitle_stream(const char *arg av_unused) { AVFormatContext *oc; if (nb_output_files <= 0) { @@ -3308,7 +3328,7 @@ #endif }
-static void opt_show_formats(void) +static void opt_show_formats(const char *arg av_unused) { AVInputFormat *ifmt=NULL; AVOutputFormat *ofmt=NULL; @@ -3503,7 +3523,7 @@ av_opt_show(sws_opts, NULL); }
-static void opt_show_help(void) +static void opt_show_help(const char *arg av_unused) { show_help(); exit(0); @@ -3660,7 +3680,7 @@ vstats_filename=av_strdup (arg); }
-static void opt_vstats (void) +static void opt_vstats (const char *arg av_unused) { char filename[40]; time_t today2 = time(NULL);
this is ugly, either way, it should be a sperate patch [...]
@@ -3702,125 +3724,125 @@
const OptionDef options[] = { /* main options */ - { "L", 0, {(void*)opt_show_license}, "show license" }, - { "h", 0, {(void*)opt_show_help}, "show help" }, - { "version", 0, {(void*)opt_show_version}, "show version" }, - { "formats", 0, {(void*)opt_show_formats}, "show available formats, codecs, protocols, ..." }, - { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" }, - { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" }, - { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" }, [...] + { "L", 0, {func_arg: opt_show_license}, "show license" }, + { "h", 0, {func_arg: opt_show_help}, "show help" }, + { "version", 0, {func_arg: opt_show_version}, "show version" }, + { "formats", 0, {func_arg: opt_show_formats}, "show available formats, codecs, protocols, ..." }, + { "f", HAS_ARG, {func_arg: opt_format}, "force format", "fmt" }, + { "i", HAS_ARG, {func_arg: opt_input_file}, "input file name", "filename" }, + { "y", OPT_BOOL, {int_arg: &file_overwrite}, "overwrite output files" },
changing (void*) to the nicer stuff is a cosmetic change and MUST be a seperate patch [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There will always be a question for which you do not know the correct awnser.
Michael Niedermayer <michaelni at gmx.at> writes:
On Wed, Jan 02, 2008 at 12:25:22AM +0100, Morten Hustveit wrote:
Hello,
during use I discovered that my -b and -vb options were rejected by ffmpeg. In the `options' array, the `opt_bitrate' function is declared to be a OPT_FUNC2 handler, yet it void. OPT_FUNC2 handlers are supposed to return int, and negative values for failures.
This is symptomatic of lack of type safety; the compiler could have warned about passing an incorrect function pointer type if given the chance, but in every case the pointers were casted to (void*). I have attached a patch which establishes type safety in the `options' array, in addition to removing all the warnings this caused.
+ { "L", 0, {func_arg: opt_show_license}, "show license" }, + { "h", 0, {func_arg: opt_show_help}, "show help" }, + { "version", 0, {func_arg: opt_show_version}, "show version" }, + { "formats", 0, {func_arg: opt_show_formats}, "show available formats, codecs, protocols, ..." }, + { "f", HAS_ARG, {func_arg: opt_format}, "force format", "fmt" }, + { "i", HAS_ARG, {func_arg: opt_input_file}, "input file name", "filename" }, + { "y", OPT_BOOL, {int_arg: &file_overwrite}, "overwrite output files" },
changing (void*) to the nicer stuff is a cosmetic change and MUST be a seperate patch
That syntax is non-standard. The proper syntax is { .foo = bar }. -- M?ns Rullg?rd mans at mansr.com
On Wed, Jan 02, 2008 at 12:49:09AM +0000, M?ns Rullg?rd wrote:
+ { "L", 0, {func_arg: opt_show_license}, "show license" }, + { "h", 0, {func_arg: opt_show_help}, "show help" }, + { "version", 0, {func_arg: opt_show_version}, "show version" }, + { "formats", 0, {func_arg: opt_show_formats}, "show available formats, codecs, protocols, ..." }, + { "f", HAS_ARG, {func_arg: opt_format}, "force format", "fmt" }, + { "i", HAS_ARG, {func_arg: opt_input_file}, "input file name", "filename" }, + { "y", OPT_BOOL, {int_arg: &file_overwrite}, "overwrite output files" },
changing (void*) to the nicer stuff is a cosmetic change and MUST be a seperate patch
That syntax is non-standard. The proper syntax is { .foo = bar }.
Is there any way to get gcc to give warnings (or better errors) for using obsolete nonstandard gccisms like this? Rich
On Tue, Jan 01, 2008 at 08:17:44PM -0500, Rich Felker wrote:
Is there any way to get gcc to give warnings (or better errors) for using obsolete nonstandard gccisms like this?
"-pedantic" will cause it to emit a warning for the old initialization syntax. But then you'll also need something like "-std=c99" to prevent it from warning about the new initialization syntax. And while I haven't actually tried it on the codebase, using those two together will cause gcc to be start being pretty strict about what it will accept, and may produce other unwanted warnings. -Dave Dodge
On Wed, Jan 02, 2008 at 12:46:38AM +0100, Michael Niedermayer wrote:
@@ -204,7 +204,7 @@ static int nb_frames_dup = 0; static int nb_frames_drop = 0; static int input_sync; -static uint64_t limit_filesize = 0; // +static int64_t limit_filesize = 0; //
filesize is not signed
While negative values make no sense, the type derives from off_t which is necessarily signed, so it doesn't really matter. Rich
On Wed, Jan 02, 2008 at 12:46:38AM +0100, Michael Niedermayer wrote:
adding const to stuff should be a seperate patch [...] fixing the return type should also be a seperate patch [...] this is ugly, either way, it should be a sperate patch [...] changing (void*) to the nicer stuff is a cosmetic change and MUST be a seperate patch
I do not agree with your patch granularity preferences. The largest patch depends on each of the small patches, but was written first. The small patches are mistakes that were discovered by the "cosmetic change" (excluding opt_bitrate, which was discovered in advance). Either way, I have split the patch in the suggested fashion.
-static uint64_t limit_filesize = 0; // +static int64_t limit_filesize = 0; //
filesize is not signed
Signed is Correct: - The variable is used only in signed contexts. - ffmpeg's offset_t is signed. - In POSIX, off_t is used for file sizes, and off_t is signed. [1] Signed is Sufficient[tm]: If you are going to limit the file size, it will be to less than 8.5 billion gigabytes in at least 9 cases out of 10.
-static void opt_new_audio_stream(void) +static void opt_new_audio_stream(const char *arg av_unused)
this is ugly, [...]
You have four choices: 1. Use the current incorrect function signature and hide the fact by emulating implicit casts with an explicit void* cast. 2. Make the current function signature correct by adding yet another option type to optutils.h. 3. Use the correct function signature and disregard the parameter. 4. Ignore compiler warnings. I find #1, #2 and #4 uglier than #3. #1 and #4 makes bugs harder to spot (opt_bitrate is an example of such bug). #2 increases the number of code lines without adding functionality or fixing bugs. 1. http://www.opengroup.org/onlinepubs/007908775/xsh/systypes.h.html -- Morten Hustveit
On Wed, Jan 02, 2008 at 02:20:50AM +0100, Morten Hustveit wrote:
On Wed, Jan 02, 2008 at 12:46:38AM +0100, Michael Niedermayer wrote:
adding const to stuff should be a seperate patch [...] fixing the return type should also be a seperate patch [...] this is ugly, either way, it should be a sperate patch [...] changing (void*) to the nicer stuff is a cosmetic change and MUST be a seperate patch
I do not agree with your patch granularity preferences. The largest patch depends on each of the small patches, but was written first. The small patches are mistakes that were discovered by the "cosmetic change" (excluding opt_bitrate, which was discovered in advance).
Either way, I have split the patch in the suggested fashion.
thanks
-static uint64_t limit_filesize = 0; // +static int64_t limit_filesize = 0; //
filesize is not signed
Signed is Correct:
- The variable is used only in signed contexts. - ffmpeg's offset_t is signed. - In POSIX, off_t is used for file sizes, and off_t is signed. [1]
Signed is Sufficient[tm]:
If you are going to limit the file size, it will be to less than 8.5 billion gigabytes in at least 9 cases out of 10.
Files have a size >= 0 in at least 9 out of 10 cases, in the remaining cases you better had a backup Anyway if people want it signed so be it, it doesnt change anything, theres no bug it fixes ...
-static void opt_new_audio_stream(void) +static void opt_new_audio_stream(const char *arg av_unused)
this is ugly, [...]
You have four choices:
1. Use the current incorrect function signature and hide the fact by emulating implicit casts with an explicit void* cast. 2. Make the current function signature correct by adding yet another option type to optutils.h. 3. Use the correct function signature and disregard the parameter. 4. Ignore compiler warnings.
I find #1, #2 and #4 uglier than #3. #1 and #4 makes bugs harder to spot (opt_bitrate is an example of such bug). #2 increases the number of code lines without adding functionality or fixing bugs.
#3 will only hide warnings with gcc as compiler, i consider using compiler specific things like av_unused ugly, especially if its unneeded. Also i consider it ugly to have options wihout HAS_ARG which then have a function ptr with an arument. [...] [const patch] ok
Index: ffmpeg.c =================================================================== --- ffmpeg.c (revision 11364) +++ ffmpeg.c (working copy) @@ -204,7 +204,7 @@ static int nb_frames_dup = 0; static int nb_frames_drop = 0; static int input_sync; -static uint64_t limit_filesize = 0; // +static int64_t limit_filesize = 0; //
static int pgmyuv_compatibility_hack=0; static float dts_delta_threshold = 10;
see above
Index: ffmpeg.c =================================================================== --- ffmpeg.c (revision 11364) +++ ffmpeg.c (working copy) @@ -3092,7 +3112,7 @@ subtitle_stream_copy = 0; }
-static void opt_new_audio_stream(void) +static void opt_new_audio_stream(const char *arg av_unused) [...]
Ill wait and see if we can come up with a nice solution before approving this ... [return type patch] ok [...]
Index: ffmpeg.c =================================================================== --- ffmpeg.c (revision 11364) +++ ffmpeg.c (working copy) @@ -3702,125 +3724,125 @@
const OptionDef options[] = { /* main options */ - { "L", 0, {(void*)opt_show_license}, "show license" }, - { "h", 0, {(void*)opt_show_help}, "show help" }, - { "version", 0, {(void*)opt_show_version}, "show version" }, - { "formats", 0, {(void*)opt_show_formats}, "show available formats, codecs, protocols, ..." }, - { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" }, - { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" }, - { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" }, - { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream[:syncfile:syncstream]" }, - { "map_meta_data", HAS_ARG | OPT_EXPERT, {(void*)opt_map_meta_data}, "set meta data information of outfile from infile", "outfile:infile" }, - { "t", HAS_ARG, {(void*)opt_recording_time}, "record or transcode \"duration\" seconds of audio/video", "duration" }, - { "fs", HAS_ARG | OPT_INT64, {(void*)&limit_filesize}, "set the limit file size in bytes", "limit_size" }, // - { "ss", HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" }, - { "itsoffset", HAS_ARG, {(void*)opt_input_ts_offset}, "set the input ts offset", "time_off" }, - { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" }, - { "timestamp", HAS_ARG, {(void*)&opt_rec_timestamp}, "set the timestamp", "time" }, - { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" }, - { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" }, - { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" }, - { "album", HAS_ARG | OPT_STRING, {(void*)&str_album}, "set the album", "string" }, - { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark}, + { "L", 0, {func_arg: opt_show_license}, "show license" },
As mans said, its non standard. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Asymptotically faster algorithms should always be preferred if you have asymptotical amounts of data
participants (6)
-
benoit.fouet@purplelabs.com -
dalias@aerifal.cx -
dododge@dododge.net -
lists@rashbox.org -
mans@mansr.com -
michaelni@gmx.at