[FFmpeg-devel,2/2] avutil/file: allow mapping 0 byte files with av_file_map

Submitted by Marton Balint on Sept. 6, 2018, 6:58 p.m.

Details

Message ID 20180906185837.24645-2-cus@passwd.hu
State New
Headers show

Commit Message

Marton Balint Sept. 6, 2018, 6:58 p.m.
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavutil/file.c | 8 ++++++++
 libavutil/file.h | 2 ++
 2 files changed, 10 insertions(+)

Comments

Michael Niedermayer Sept. 7, 2018, 10:29 a.m.
On Thu, Sep 06, 2018 at 08:58:37PM +0200, Marton Balint wrote:
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  libavutil/file.c | 8 ++++++++
>  libavutil/file.h | 2 ++
>  2 files changed, 10 insertions(+)

probably ok

thx

[...]
Marton Balint Sept. 9, 2018, 7:33 p.m.
On Fri, 7 Sep 2018, Michael Niedermayer wrote:

> On Thu, Sep 06, 2018 at 08:58:37PM +0200, Marton Balint wrote:
>> Signed-off-by: Marton Balint <cus@passwd.hu>
>> ---
>>  libavutil/file.c | 8 ++++++++
>>  libavutil/file.h | 2 ++
>>  2 files changed, 10 insertions(+)
>
> probably ok

Thanks, applied.

Regards,
Marton

Patch hide | download patch | download mbox

diff --git a/libavutil/file.c b/libavutil/file.c
index 2153e51379..64f11fed76 100644
--- a/libavutil/file.c
+++ b/libavutil/file.c
@@ -85,6 +85,11 @@  int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
     }
     *size = off_size;
 
+    if (!*size) {
+        *bufptr = NULL;
+        goto out;
+    }
+
 #if HAVE_MMAP
     ptr = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
     if (ptr == MAP_FAILED) {
@@ -126,12 +131,15 @@  int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
     read(fd, *bufptr, *size);
 #endif
 
+out:
     close(fd);
     return 0;
 }
 
 void av_file_unmap(uint8_t *bufptr, size_t size)
 {
+    if (!size)
+        return;
 #if HAVE_MMAP
     munmap(bufptr, size);
 #elif HAVE_MAPVIEWOFFILE
diff --git a/libavutil/file.h b/libavutil/file.h
index 8666c7b1d5..ea6798b259 100644
--- a/libavutil/file.h
+++ b/libavutil/file.h
@@ -33,6 +33,8 @@ 
  * allocated buffer or map it with mmap() when available.
  * In case of success set *bufptr to the read or mmapped buffer, and
  * *size to the size in bytes of the buffer in *bufptr.
+ * Unlike mmap this function succeeds with zero sized files, in this
+ * case *bufptr will be be NULL and *size will be set to 0.
  * The returned buffer must be released with av_file_unmap().
  *
  * @param log_offset loglevel offset used for logging