[FFmpeg-devel] [PATCH 6/9] lavf/ftp: check for truncation in snprintf

Anton Khirnov anton at khirnov.net
Thu Nov 25 17:04:57 EET 2021


Silences e.g. the following warning in gcc 10:
src/libavformat/ftp.c: In function ‘ftp_move’:
src/libavformat/ftp.c:1122:46: warning: ‘%s’ directive output may be truncated writing up to 4095 bytes into a region of size 4091 [-Wformat-truncation=]
 1122 |     snprintf(command, sizeof(command), "RNTO %s\r\n", path);
      |                                              ^~       ~~~~
src/libavformat/ftp.c:1122:5: note: ‘snprintf’ output between 8 and 4103 bytes into a destination of size 4096
 1122 |     snprintf(command, sizeof(command), "RNTO %s\r\n", path);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 libavformat/ftp.c | 64 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 52 insertions(+), 12 deletions(-)

diff --git a/libavformat/ftp.c b/libavformat/ftp.c
index 69caa7670c..883668b37b 100644
--- a/libavformat/ftp.c
+++ b/libavformat/ftp.c
@@ -250,13 +250,19 @@ static int ftp_auth(FTPContext *s)
 
     if (strpbrk(s->user, "\r\n"))
         return AVERROR(EINVAL);
-    snprintf(buf, sizeof(buf), "USER %s\r\n", s->user);
+    err = snprintf(buf, sizeof(buf), "USER %s\r\n", s->user);
+    if (err >= sizeof(buf))
+        return AVERROR(ENOSYS);
+
     err = ftp_send_command(s, buf, user_codes, NULL);
     if (err == 331) {
         if (s->password) {
             if (strpbrk(s->password, "\r\n"))
                 return AVERROR(EINVAL);
-            snprintf(buf, sizeof(buf), "PASS %s\r\n", s->password);
+            err = snprintf(buf, sizeof(buf), "PASS %s\r\n", s->password);
+            if (err >= sizeof(buf))
+                return AVERROR(ENOSYS);
+
             err = ftp_send_command(s, buf, pass_codes, NULL);
         } else
             return AVERROR(EACCES);
@@ -397,9 +403,13 @@ static int ftp_file_size(FTPContext *s)
 {
     char command[CONTROL_BUFFER_SIZE];
     char *res = NULL;
+    int ret;
     static const int size_codes[] = {213, 0};
 
-    snprintf(command, sizeof(command), "SIZE %s\r\n", s->path);
+    ret = snprintf(command, sizeof(command), "SIZE %s\r\n", s->path);
+    if (ret >= sizeof(command))
+        return AVERROR(ENOSYS);
+
     if (ftp_send_command(s, command, size_codes, &res) == 213 && res && strlen(res) > 4) {
         s->filesize = strtoll(&res[4], NULL, 10);
     } else {
@@ -416,9 +426,12 @@ static int ftp_retrieve(FTPContext *s)
 {
     char command[CONTROL_BUFFER_SIZE];
     static const int retr_codes[] = {150, 125, 0};
-    int resp_code;
+    int resp_code, ret;
+
+    ret = snprintf(command, sizeof(command), "RETR %s\r\n", s->path);
+    if (ret >= sizeof(command))
+        return AVERROR(ENOSYS);
 
-    snprintf(command, sizeof(command), "RETR %s\r\n", s->path);
     resp_code = ftp_send_command(s, command, retr_codes, NULL);
     if (resp_code != 125 && resp_code != 150)
         return AVERROR(EIO);
@@ -432,9 +445,12 @@ static int ftp_store(FTPContext *s)
 {
     char command[CONTROL_BUFFER_SIZE];
     static const int stor_codes[] = {150, 125, 0};
-    int resp_code;
+    int resp_code, ret;
+
+    ret = snprintf(command, sizeof(command), "STOR %s\r\n", s->path);
+    if (ret >= sizeof(command))
+        return AVERROR(ENOSYS);
 
-    snprintf(command, sizeof(command), "STOR %s\r\n", s->path);
     resp_code = ftp_send_command(s, command, stor_codes, NULL);
     if (resp_code != 125 && resp_code != 150)
         return AVERROR(EIO);
@@ -471,8 +487,12 @@ static int ftp_set_dir(FTPContext *s)
 {
     static const int cwd_codes[] = {250, 550, 0}; /* 550 is incorrect code */
     char command[MAX_URL_SIZE];
+    int ret;
+
+    ret = snprintf(command, sizeof(command), "CWD %s\r\n", s->path);
+    if (ret >= sizeof(command))
+        return AVERROR(ENOSYS);
 
-    snprintf(command, sizeof(command), "CWD %s\r\n", s->path);
     if (ftp_send_command(s, command, cwd_codes, NULL) != 250)
         return AVERROR(EIO);
     return 0;
@@ -1082,13 +1102,23 @@ static int ftp_delete(URLContext *h)
     if ((ret = ftp_connect(h, h->filename)) < 0)
         goto cleanup;
 
-    snprintf(command, sizeof(command), "DELE %s\r\n", s->path);
+    ret = snprintf(command, sizeof(command), "DELE %s\r\n", s->path);
+    if (ret >= sizeof(command)) {
+        ret = AVERROR(ENOSYS);
+        goto cleanup;
+    }
+
     if (ftp_send_command(s, command, del_codes, NULL) == 250) {
         ret = 0;
         goto cleanup;
     }
 
-    snprintf(command, sizeof(command), "RMD %s\r\n", s->path);
+    ret = snprintf(command, sizeof(command), "RMD %s\r\n", s->path);
+    if (ret >= sizeof(command)) {
+        ret = AVERROR(ENOSYS);
+        goto cleanup;
+    }
+
     if (ftp_send_command(s, command, rmd_codes, NULL) == 250)
         ret = 0;
     else
@@ -1110,7 +1140,12 @@ static int ftp_move(URLContext *h_src, URLContext *h_dst)
     if ((ret = ftp_connect(h_src, h_src->filename)) < 0)
         goto cleanup;
 
-    snprintf(command, sizeof(command), "RNFR %s\r\n", s->path);
+    ret = snprintf(command, sizeof(command), "RNFR %s\r\n", s->path);
+    if (ret >= sizeof(command)) {
+        ret = AVERROR(ENOSYS);
+        goto cleanup;
+    }
+
     if (ftp_send_command(s, command, rnfr_codes, NULL) != 350) {
         ret = AVERROR(EIO);
         goto cleanup;
@@ -1119,7 +1154,12 @@ static int ftp_move(URLContext *h_src, URLContext *h_dst)
     av_url_split(0, 0, 0, 0, 0, 0, 0,
                  path, sizeof(path),
                  h_dst->filename);
-    snprintf(command, sizeof(command), "RNTO %s\r\n", path);
+    ret = snprintf(command, sizeof(command), "RNTO %s\r\n", path);
+    if (ret >= sizeof(command)) {
+        ret = AVERROR(ENOSYS);
+        goto cleanup;
+    }
+
     if (ftp_send_command(s, command, rnto_codes, NULL) == 250)
         ret = 0;
     else
-- 
2.33.0



More information about the ffmpeg-devel mailing list