[FFmpeg-devel] [PATCH v2 1/4] avutil/avstring: support input path is a null pointer or empty string

lance.lmwang at gmail.com lance.lmwang at gmail.com
Tue Sep 17 07:39:24 EEST 2019


From: Limin Wang <lance.lmwang at gmail.com>

Reviewed-by: Tomas Härdin <tjoppen at acc.umu.se>
Reviewed-by: Liu Steven <lq at chinaffmpeg.org>
Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
---
 libavutil/avstring.c | 12 ++++++++----
 libavutil/avstring.h | 13 +++++++++----
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/libavutil/avstring.c b/libavutil/avstring.c
index 4c068f5bc5..551ca5daee 100644
--- a/libavutil/avstring.c
+++ b/libavutil/avstring.c
@@ -257,8 +257,12 @@ char *av_strireplace(const char *str, const char *from, const char *to)
 
 const char *av_basename(const char *path)
 {
-    char *p = strrchr(path, '/');
+    char *p;
+
+    if (!path || *path == '\0')
+        return ".";
 
+    p = strrchr(path, '/');
 #if HAVE_DOS_PATHS
     char *q = strrchr(path, '\\');
     char *d = strchr(path, ':');
@@ -274,11 +278,11 @@ const char *av_basename(const char *path)
 
 const char *av_dirname(char *path)
 {
-    char *p = strrchr(path, '/');
+    char *p = path ? strrchr(path, '/') : NULL;
 
 #if HAVE_DOS_PATHS
-    char *q = strrchr(path, '\\');
-    char *d = strchr(path, ':');
+    char *q = path ? strrchr(path, '\\') : NULL;
+    char *d = path ? strchr(path, ':')  : NULL;
 
     d = d ? d + 1 : d;
 
diff --git a/libavutil/avstring.h b/libavutil/avstring.h
index 37dd4e2da0..274335cfb9 100644
--- a/libavutil/avstring.h
+++ b/libavutil/avstring.h
@@ -274,16 +274,21 @@ char *av_strireplace(const char *str, const char *from, const char *to);
 
 /**
  * Thread safe basename.
- * @param path the path, on DOS both \ and / are considered separators.
+ * @param path the string to parse, on DOS both \ and / are considered separators.
  * @return pointer to the basename substring.
+ * If path does not contain a slash, the function returns a copy of path.
+ * If path is a NULL pointer or points to an empty string, a pointer
+ * to a string "." is returned.
  */
 const char *av_basename(const char *path);
 
 /**
  * Thread safe dirname.
- * @param path the path, on DOS both \ and / are considered separators.
- * @return the path with the separator replaced by the string terminator or ".".
- * @note the function may change the input string.
+ * @param path the string to parse, on DOS both \ and / are considered separators.
+ * @return A pointer to a string that's the parent directory of path.
+ * If path is a NULL pointer or points to an empty string, a pointer
+ * to a string "." is returned.
+ * @note the function may modify the contents of the path, so copies should be passed.
  */
 const char *av_dirname(char *path);
 
-- 
2.21.0



More information about the ffmpeg-devel mailing list