diff mbox series

[FFmpeg-devel,1/2] avformat/hlsenc: Redo checking for strftime %s support to avoid warnings

Message ID AS8P250MB07446F23609CF3A328B03D298F222@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit d085f341d66ebfe5b29d67773e89c253f4eb99a8
Headers show
Series [FFmpeg-devel,1/2] avformat/hlsenc: Redo checking for strftime %s support to avoid warnings | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt March 5, 2024, 10:27 a.m. UTC
This is intended to avoid -Wformat= warnings on systems
where %s might not be supported (and also generally emitted
by GCC with -pedantic).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/hlsenc.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

Comments

Liu Steven March 5, 2024, 12:54 p.m. UTC | #1
> On Mar 5, 2024, at 18:27, Andreas Rheinhardt <andreas.rheinhardt@outlook.com> wrote:
> 
> This is intended to avoid -Wformat= warnings on systems
> where %s might not be supported (and also generally emitted
> by GCC with -pedantic).
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
> libavformat/hlsenc.c | 19 ++++++++++++-------
> 1 file changed, 12 insertions(+), 7 deletions(-)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index a19b9bb3d1..d5cd627e59 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -1878,18 +1878,23 @@ fail:
> 
> static const char * get_default_pattern_localtime_fmt(AVFormatContext *s)
> {
> +    HLSContext *hls = s->priv_data;
> +#if HAVE_LIBC_MSVCRT
> +    // no %s support on MSVC, which invokes the invalid parameter handler
> +    // on unsupported format strings, instead of returning an error
> +    int strftime_s_supported = 0;
> +#else
>     char b[21];
>     time_t t = time(NULL);
> -    struct tm *p, tmbuf;
> -    HLSContext *hls = s->priv_data;
> -
> -    p = localtime_r(&t, &tmbuf);
> +    struct tm tmbuf, *p = localtime_r(&t, &tmbuf);
>     // no %s support when strftime returned error or left format string unchanged
> -    // also no %s support on MSVC, which invokes the invalid parameter handler on unsupported format strings, instead of returning an error
> +    int strftime_s_supported = strftime(b, sizeof(b), "%s", p) && strcmp(b, "%s");
> +#endif
> +
>     if (hls->segment_type == SEGMENT_TYPE_FMP4) {
> -        return (HAVE_LIBC_MSVCRT || !strftime(b, sizeof(b), "%s", p) || !strcmp(b, "%s")) ? "-%Y%m%d%H%M%S.m4s" : "-%s.m4s";
> +        return strftime_s_supported ? "-%s.m4s" : "-%Y%m%d%H%M%S.m4s";
>     }
> -    return (HAVE_LIBC_MSVCRT || !strftime(b, sizeof(b), "%s", p) || !strcmp(b, "%s")) ? "-%Y%m%d%H%M%S.ts" : "-%s.ts";
> +    return strftime_s_supported ? "-%s.ts" : "-%Y%m%d%H%M%S.ts";
> }
> 
> static int append_postfix(char *name, int name_buf_len, int i)
> -- 
> 2.40.1
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe”.
patchset looks good to me


Thanks
Steven
diff mbox series

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index a19b9bb3d1..d5cd627e59 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -1878,18 +1878,23 @@  fail:
 
 static const char * get_default_pattern_localtime_fmt(AVFormatContext *s)
 {
+    HLSContext *hls = s->priv_data;
+#if HAVE_LIBC_MSVCRT
+    // no %s support on MSVC, which invokes the invalid parameter handler
+    // on unsupported format strings, instead of returning an error
+    int strftime_s_supported = 0;
+#else
     char b[21];
     time_t t = time(NULL);
-    struct tm *p, tmbuf;
-    HLSContext *hls = s->priv_data;
-
-    p = localtime_r(&t, &tmbuf);
+    struct tm tmbuf, *p = localtime_r(&t, &tmbuf);
     // no %s support when strftime returned error or left format string unchanged
-    // also no %s support on MSVC, which invokes the invalid parameter handler on unsupported format strings, instead of returning an error
+    int strftime_s_supported = strftime(b, sizeof(b), "%s", p) && strcmp(b, "%s");
+#endif
+
     if (hls->segment_type == SEGMENT_TYPE_FMP4) {
-        return (HAVE_LIBC_MSVCRT || !strftime(b, sizeof(b), "%s", p) || !strcmp(b, "%s")) ? "-%Y%m%d%H%M%S.m4s" : "-%s.m4s";
+        return strftime_s_supported ? "-%s.m4s" : "-%Y%m%d%H%M%S.m4s";
     }
-    return (HAVE_LIBC_MSVCRT || !strftime(b, sizeof(b), "%s", p) || !strcmp(b, "%s")) ? "-%Y%m%d%H%M%S.ts" : "-%s.ts";
+    return strftime_s_supported ? "-%s.ts" : "-%Y%m%d%H%M%S.ts";
 }
 
 static int append_postfix(char *name, int name_buf_len, int i)