Blob Blame History Raw
From a93c17b054313f8ce0b0474942177a3aa646893b Mon Sep 17 00:00:00 2001
From: Mamoru TASAKA <mtasaka@fedoraproject.org>
Date: Mon, 28 Dec 2020 15:29:41 +0900
Subject: [PATCH 5/6] recanim.c: fix up -Wformat-overflow warning:

From gcc11 -Wall:
--------------------------------------------------------------
../../hacks/recanim.c: In function 'screenhack_record_anim_free':
../../hacks/recanim.c:435:12: warning: '%s' directive writing up to 1023 bytes into a region of size 968 [-Wformat-overflow=]
  435 |            " -c:v libx264"
      |            ^~~~~~~~~~~~~~~
......
  442 |            fn);
      |            ~~
../../hacks/recanim.c:439:15: note: format string is defined here
  439 |            " '%s'"
      |               ^~
--------------------------------------------------------------

To silence this warning, calculate the size of the string which is "supposed to be"
written and if the size is going to be oversized, exit abnormally.
---
 hacks/recanim.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/hacks/recanim.c b/hacks/recanim.c
index b1c7e0a..bf9084c 100644
--- a/hacks/recanim.c
+++ b/hacks/recanim.c
@@ -391,6 +391,7 @@ screenhack_record_anim_free (record_anim_state *st)
   const char *type = "png";
   char cmd[1024];
   char fn[1024];
+  size_t len_cmd;
   const char *soundtrack = 0;
 
   fprintf (stderr, "%s: wrote %d frames\n", progname, st->frame_count);
@@ -417,7 +418,8 @@ screenhack_record_anim_free (record_anim_state *st)
   if (! soundtrack) soundtrack = "../../" ST;
   if (stat (soundtrack, &s)) soundtrack = 0;
 
-  sprintf (cmd,
+  len_cmd = 0;
+  len_cmd += snprintf (cmd, sizeof cmd - len_cmd,
            "ffmpeg"
            " -hide_banner"
            " -loglevel error"
@@ -425,13 +427,16 @@ screenhack_record_anim_free (record_anim_state *st)
            " -i '%s-%%06d.%s'"
            " -r %d",		/* rate of output: must be after -i */
            st->fps, progname, type, st->fps);
-  if (soundtrack)
-    sprintf (cmd + strlen(cmd),
+  if (len_cmd >= sizeof cmd) abort();
+  if (soundtrack) {
+    len_cmd += snprintf (cmd + len_cmd, sizeof cmd - len_cmd,
              " -i '%s' -map 0:v:0 -map 1:a:0 -acodec aac"
              /* Truncate or pad audio to length of video */
              " -filter_complex '[1:0] apad' -shortest",
              soundtrack);
-  sprintf (cmd + strlen(cmd),
+    if (len_cmd >= sizeof cmd) abort();
+  }
+  len_cmd += snprintf (cmd + len_cmd, sizeof cmd - len_cmd,
            " -c:v libx264"
            " -profile:v high"
            " -crf 18"
@@ -440,6 +445,7 @@ screenhack_record_anim_free (record_anim_state *st)
            " </dev/null"
            /*" 2>&-"*/,
            fn);
+  if (len_cmd >= sizeof cmd) abort();
   fprintf (stderr, "%s: exec: %s\n", progname, cmd);
   /* Use empty body to kill warning from gcc -Wall with
      "warning: ignoring return value of 'system',
-- 
2.29.2