Paste

URL To paste - | raw - Thu Mar 31 2022 22:22:10 GMT+0000 (Coordinated Universal Time)
diff --git a/libavformat/ipfsgateway.c b/libavformat/ipfsgateway.c
index 1a039589c0..9cddf9f055 100644
--- a/libavformat/ipfsgateway.c
+++ b/libavformat/ipfsgateway.c
@@ -19,22 +19,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "avformat.h"
-#include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
-#include "libavutil/internal.h"
 #include "libavutil/opt.h"
-#include "libavutil/tree.h"
-#include 
-#if HAVE_IO_H
-#include 
-#endif
-#if HAVE_UNISTD_H
-#include 
-#endif
-#include "os_support.h"
 #include "url.h"
-#include 
 #include 
 
 typedef struct IPFSGatewayContext {
@@ -137,10 +124,11 @@ static int populate_ipfs_gateway(URLContext *h)
     }
 
     // Read a single line (fgets stops at new line mark).
-    fgets(c->gateway_buffer, sizeof(c->gateway_buffer) - 1, gateway_file);
-
-    // Replace the last char with \0
-    c->gateway_buffer[sizeof(c->gateway_buffer) - 1] = 0;
+    if (!fgets(c->gateway_buffer, sizeof(c->gateway_buffer) - 1, gateway_file)) {
+      av_log(h, AV_LOG_WARNING, "Unable to read from file (full uri: %s).\n", ipfs_gateway_file);
+      ret = AVERROR(ENOENT);
+      goto err;
+    }
 
     // Replace first occurence of end of line with \0
     c->gateway_buffer[strcspn(c->gateway_buffer, "\r")] = 0;
@@ -148,7 +136,7 @@ static int populate_ipfs_gateway(URLContext *h)
 
     // If strlen finds anything longer then 0 characters then we have a
     // potential gateway url.
-    if (strlen(c->gateway_buffer) < 1) {
+    if (*c->gateway_buffer == '\0') {
         av_log(h, AV_LOG_WARNING, "The IPFS gateway file (full uri: %s) appears to be empty. Is the gateway started?\n", ipfs_gateway_file);
         ret = AVERROR(EILSEQ);
         goto err;
@@ -212,11 +200,11 @@ static int translate_ipfs_to_http(URLContext *h, const char *uri,
             snprintf(c->gateway_buffer, sizeof(c->gateway_buffer), "https://dweb.link");
 
             av_log(h, AV_LOG_WARNING, "IPFS does not appear to be running. You’re now using the public gateway at dweb.link.\n");
-            av_log(h, AV_LOG_INFO, "Installing IPFS locally is recommended to improve performance and reliability, and not share all your activity with a single IPFS gateway.\n");
-            av_log(h, AV_LOG_INFO, "There are multiple options to define this gateway.\n");
-            av_log(h, AV_LOG_INFO, "1. Call ffmpeg with a gateway param, without a trailing slash: -gateway .\n");
-            av_log(h, AV_LOG_INFO, "2. Define an $IPFS_GATEWAY environment variable with the full HTTP URL to the gateway without trailing forward slash.\n");
-            av_log(h, AV_LOG_INFO, "3. Define an $IPFS_PATH environment variable and point it to the IPFS data path - this is typically ~/.ipfs\n");
+            av_log(h, AV_LOG_INFO, "Installing IPFS locally is recommended to improve performance and reliability, and not share all your activity with a single IPFS gateway.\n"
+                   "There are multiple options to define this gateway.\n"
+                   "1. Call ffmpeg with a gateway param, without a trailing slash: -gateway .\n"
+                   "2. Define an $IPFS_GATEWAY environment variable with the full HTTP URL to the gateway without trailing forward slash.\n"
+                   "3. Define an $IPFS_PATH environment variable and point it to the IPFS data path - this is typically ~/.ipfs\n");
         }
     }
 
@@ -241,12 +229,18 @@ static int translate_ipfs_to_http(URLContext *h, const char *uri,
                           (is_ipns) ? "ipns/" : "ipfs/",
                           ipfs_cid);
 
+    if (!fulluri) {
+      av_log(h, AV_LOG_ERROR, "Failed to compose the URL\n");
+      ret = AVERROR(ENOMEM);
+      goto err;
+    }
+
     // Pass the URL back to FFMpeg's protocol handler.
-    if ((ret = ffurl_open_whitelist(&c->inner, fulluri, flags,
-                                    &h->interrupt_callback, options,
-                                    h->protocol_whitelist,
-                                    h->protocol_blacklist, h))
-        < 0) {
+    ret = ffurl_open_whitelist(&c->inner, fulluri, flags,
+                               &h->interrupt_callback, options,
+                               h->protocol_whitelist,
+                               h->protocol_blacklist, h);
+    if (ret < 0) {
         av_log(h, AV_LOG_WARNING, "Unable to open resource: %s\n", fulluri);
         goto err;
     }