summaryrefslogtreecommitdiff
path: root/media-libs/libmp4v2/files/libmp4v2-2.1.3-mem-leaks-1.patch
blob: a12c24f4e3feb0974030ff68a0599796607d4d79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
From c724815a541b763455ff38922af96f652627bce6 Mon Sep 17 00:00:00 2001
From: Robert Kausch <robert.kausch@freac.org>
Date: Tue, 16 May 2023 00:19:02 +0200
Subject: [PATCH] Fix memory leaks in case MP4File::ReadBytes() throws an
 exception.

--- a/src/atom_rtp.cpp
+++ b/src/atom_rtp.cpp
@@ -125,12 +125,19 @@ void MP4RtpAtom::ReadHntiType()
 
     // read sdp string, length is implicit in size of atom
     uint64_t size = GetEnd() - m_File.GetPosition();
-    char* data = (char*)MP4Malloc(size + 1);
+    char* data = (char*) MP4Malloc(size + 1);
     ASSERT(data != NULL);
-    m_File.ReadBytes((uint8_t*)data, size);
-    data[size] = '\0';
-    ((MP4StringProperty*)m_pProperties[1])->SetValue(data);
-    MP4Free(data);
+    try {
+        m_File.ReadBytes((uint8_t*) data, size);
+        data[size] = '\0';
+        ((MP4StringProperty*) m_pProperties[1])->SetValue(data);
+        MP4Free(data);
+    }
+    catch (Exception*) {
+        // free memory and rethrow
+        MP4Free(data);
+        throw;
+    }
 }
 
 void MP4RtpAtom::Write()
--- a/src/atom_sdp.cpp
+++ b/src/atom_sdp.cpp
@@ -36,12 +36,19 @@ void MP4SdpAtom::Read()
 {
     // read sdp string, length is implicit in size of atom
     uint64_t size = GetEnd() - m_File.GetPosition();
-    char* data = (char*)MP4Malloc(size + 1);
+    char* data = (char*) MP4Malloc(size + 1);
     ASSERT(data != NULL);
-    m_File.ReadBytes((uint8_t*)data, size);
-    data[size] = '\0';
-    ((MP4StringProperty*)m_pProperties[0])->SetValue(data);
-    MP4Free(data);
+    try {
+        m_File.ReadBytes((uint8_t*) data, size);
+        data[size] = '\0';
+        ((MP4StringProperty*) m_pProperties[0])->SetValue(data);
+        MP4Free(data);
+    }
+    catch (Exception*) {
+        // free memory and rethrow
+        MP4Free(data);
+        throw;
+    }
 }
 
 void MP4SdpAtom::Write()
--- a/src/mp4file_io.cpp
+++ b/src/mp4file_io.cpp
@@ -325,19 +325,26 @@ char* MP4File::ReadString()
 {
     uint32_t length = 0;
     uint32_t alloced = 64;
-    char* data = (char*)MP4Malloc(alloced);
-
-    do {
-        if (length == alloced) {
-            data = (char*)MP4Realloc(data, alloced * 2);
-            if (data == NULL) return NULL;
-            alloced *= 2;
-        }
-        ReadBytes((uint8_t*)&data[length], 1);
-        length++;
-    } while (data[length - 1] != 0);
-
-    data = (char*)MP4Realloc(data, length);
+    char* data = (char*) MP4Malloc(alloced);
+    try {
+        do {
+            if (length == alloced) {
+                data = (char*) MP4Realloc(data, alloced * 2);
+                if (data == NULL)
+                    return NULL;
+                alloced *= 2;
+            }
+            ReadBytes((uint8_t*) &data[length], 1);
+            length++;
+        } while (data[length - 1] != 0);
+
+        data = (char*) MP4Realloc(data, length);
+    }
+    catch (Exception*) {
+        // free memory and rethrow
+        MP4Free(data);
+        throw;
+    }
     return data;
 }
 
@@ -384,21 +391,34 @@ char* MP4File::ReadCountedString(uint8_t charSize, bool allowExpandedCount, uint
     }
 
     uint32_t byteLength = charLength * charSize;
-    char* data = (char*)MP4Malloc(byteLength + 1);
-    if (byteLength > 0) {
-        ReadBytes((uint8_t*)data, byteLength);
-    }
-    data[byteLength] = '\0';
-
-    // read padding
-    if (fixedLength) {
-        const uint8_t padsize = fixedLength - byteLength -1U;
-        if( padsize ) {
-            uint8_t* padbuf = (uint8_t*)malloc( padsize );
-            ReadBytes( padbuf, padsize );
-            free( padbuf );
+    char* data = (char*) MP4Malloc(byteLength + 1);
+    try {
+        if (byteLength > 0)
+            ReadBytes((uint8_t*) data, byteLength);
+        data[byteLength] = '\0';
+
+        // read padding
+        if (fixedLength) {
+            const uint8_t padsize = fixedLength - byteLength -1U;
+            if (padsize) {
+                uint8_t* padbuf = (uint8_t*) MP4Malloc(padsize);
+                try {
+                    ReadBytes(padbuf, padsize);
+                    MP4Free(padbuf);
+                }
+                catch (Exception*) {
+                    // free memory and rethrow
+                    MP4Free(padbuf);
+                    throw;
+                }
+            }
         }
     }
+    catch (Exception*) {
+        // free memory and rethrow
+        MP4Free(data);
+        throw;
+    }
 
     return data;
 }