[vlc-commits] [Git][videolan/vlc][master] memstream: reset ptr on vlc_memstream_close() error
    Steve Lhomme (@robUx4) 
    gitlab at videolan.org
       
    Tue Nov 28 07:25:34 UTC 2023
    
    
  
Steve Lhomme pushed to branch master at VideoLAN / VLC
Commits:
e957881f by Steve Lhomme at 2023-11-28T06:59:37+00:00
memstream: reset ptr on vlc_memstream_close() error
It's easier to spot of NULL pointer dereference than a use after free.
In the POSIX implementation [1] the status of the pointer is undefined on
error. In our implementation it's free'd.
In both cases it's better some to use that pointer value after exiting
vlc_memstream_close().
[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/open_memstream.html
- - - - -
1 changed file:
- src/text/memstream.c
Changes:
=====================================
src/text/memstream.c
=====================================
@@ -49,17 +49,26 @@ int vlc_memstream_close(struct vlc_memstream *ms)
     int ret;
 
     if (unlikely(stream == NULL))
+    {
+        // was never properly opened
+        ms->ptr = NULL;
         return EOF;
+    }
 
     ms->stream = NULL;
     ret = ferror(stream);
 
     if (fclose(stream))
+    {
+        // assuming it's free'd by the memstream
+        ms->ptr = NULL;
         return EOF;
+    }
 
     if (unlikely(ret))
     {
         free(ms->ptr);
+        ms->ptr = NULL;
         return EOF;
     }
     return 0;
@@ -123,7 +132,10 @@ int vlc_memstream_flush(struct vlc_memstream *ms)
 int vlc_memstream_close(struct vlc_memstream *ms)
 {
     if (ms->error)
+    {
         free(ms->ptr);
+        ms->ptr = NULL;
+    }
     return ms->error;
 } 
 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/e957881f713b057b3841a44ec023c404544748d7
-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/e957881f713b057b3841a44ec023c404544748d7
You're receiving this email because of your account on code.videolan.org.
VideoLAN code repository instance
    
    
More information about the vlc-commits
mailing list