1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00

Now checking the result of all realloc() calls

This commit is contained in:
Klaus Schmidinger 2011-02-25 15:25:42 +01:00
parent e145ee45e2
commit d1ab9dbc5f
12 changed files with 188 additions and 77 deletions

View File

@ -6481,7 +6481,7 @@ Video Disk Recorder Revision History
from Osama Alrawab). See INSTALL for information on how to turn this on. from Osama Alrawab). See INSTALL for information on how to turn this on.
- Added Arabian language texts (thanks to Osama Alrawab). - Added Arabian language texts (thanks to Osama Alrawab).
2011-02-20: Version 1.7.17 2011-02-25: Version 1.7.17
- Updated the Estonian OSD texts (thanks to Arthur Konovalov). - Updated the Estonian OSD texts (thanks to Arthur Konovalov).
- Fixed following symbolic links in RemoveFileOrDir() (cont'd) (thanks to - Fixed following symbolic links in RemoveFileOrDir() (cont'd) (thanks to
@ -6538,3 +6538,4 @@ Video Disk Recorder Revision History
- Fixed reallocating memory in the "pictures" plugin (reported by Paul Menzel, with - Fixed reallocating memory in the "pictures" plugin (reported by Paul Menzel, with
input from Oliver Endriss). input from Oliver Endriss).
- Fixed reallocating memory in cTsToPes::PutTs() (suggested by Oliver Endriss). - Fixed reallocating memory in cTsToPes::PutTs() (suggested by Oliver Endriss).
- Now checking the result of all realloc() calls.

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: device.c 2.37 2010/06/03 13:35:02 kls Exp $ * $Id: device.c 2.38 2011/02/25 15:12:03 kls Exp $
*/ */
#include "device.h" #include "device.h"
@ -1087,11 +1087,17 @@ void cDevice::StillPicture(const uchar *Data, int Length)
int l; int l;
while (const uchar *p = TsToPes.GetPes(l)) { while (const uchar *p = TsToPes.GetPes(l)) {
int Offset = Size; int Offset = Size;
Size += l; int NewSize = Size + l;
buf = (uchar *)realloc(buf, Size); if (uchar *NewBuffer = (uchar *)realloc(buf, NewSize)) {
if (!buf) Size = NewSize;
buf = NewBuffer;
memcpy(buf + Offset, p, l);
}
else {
LOG_ERROR_STR("out of memory");
free(buf);
return; return;
memcpy(buf + Offset, p, l); }
} }
TsToPes.Reset(); TsToPes.Reset();
} }
@ -1103,11 +1109,17 @@ void cDevice::StillPicture(const uchar *Data, int Length)
int l; int l;
while (const uchar *p = TsToPes.GetPes(l)) { while (const uchar *p = TsToPes.GetPes(l)) {
int Offset = Size; int Offset = Size;
Size += l; int NewSize = Size + l;
buf = (uchar *)realloc(buf, Size); if (uchar *NewBuffer = (uchar *)realloc(buf, NewSize)) {
if (!buf) Size = NewSize;
buf = NewBuffer;
memcpy(buf + Offset, p, l);
}
else {
esyslog("ERROR: out of memory");
free(buf);
return; return;
memcpy(buf + Offset, p, l); }
} }
StillPicture(buf, Size); StillPicture(buf, Size);
free(buf); free(buf);

View File

@ -7,7 +7,7 @@
* Original author: Marco Schlüßler <marco@lordzodiac.de> * Original author: Marco Schlüßler <marco@lordzodiac.de>
* With some input from the "subtitle plugin" by Pekka Virtanen <pekka.virtanen@sci.fi> * With some input from the "subtitle plugin" by Pekka Virtanen <pekka.virtanen@sci.fi>
* *
* $Id: dvbsubtitle.c 2.8 2010/10/24 12:25:45 kls Exp $ * $Id: dvbsubtitle.c 2.9 2011/02/25 15:13:32 kls Exp $
*/ */
#include "dvbsubtitle.h" #include "dvbsubtitle.h"
@ -570,12 +570,17 @@ void cDvbSubtitleAssembler::Reset(void)
bool cDvbSubtitleAssembler::Realloc(int Size) bool cDvbSubtitleAssembler::Realloc(int Size)
{ {
if (Size > size) { if (Size > size) {
size = max(Size, 2048); Size = max(Size, 2048);
data = (uchar *)realloc(data, size); if (uchar *NewBuffer = (uchar *)realloc(data, Size)) {
if (!data) { size = Size;
data = NewBuffer;
}
else {
esyslog("ERROR: can't allocate memory for subtitle assembler"); esyslog("ERROR: can't allocate memory for subtitle assembler");
length = 0; length = 0;
size = 0; size = 0;
free(data);
data = NULL;
return false; return false;
} }
} }

27
epg.c
View File

@ -7,7 +7,7 @@
* Original version (as used in VDR before 1.3.0) written by * Original version (as used in VDR before 1.3.0) written by
* Robert Schneider <Robert.Schneider@web.de> and Rolf Hakenes <hakenes@hippomi.de>. * Robert Schneider <Robert.Schneider@web.de> and Rolf Hakenes <hakenes@hippomi.de>.
* *
* $Id: epg.c 2.6 2010/02/28 14:24:55 kls Exp $ * $Id: epg.c 2.7 2011/02/25 15:16:05 kls Exp $
*/ */
#include "epg.h" #include "epg.h"
@ -56,25 +56,34 @@ cComponents::~cComponents(void)
free(components); free(components);
} }
void cComponents::Realloc(int Index) bool cComponents::Realloc(int Index)
{ {
if (Index >= numComponents) { if (Index >= numComponents) {
int n = numComponents; Index++;
numComponents = Index + 1; if (tComponent *NewBuffer = (tComponent *)realloc(components, Index * sizeof(tComponent))) {
components = (tComponent *)realloc(components, numComponents * sizeof(tComponent)); int n = numComponents;
memset(&components[n], 0, sizeof(tComponent) * (numComponents - n)); numComponents = Index;
components = NewBuffer;
memset(&components[n], 0, sizeof(tComponent) * (numComponents - n));
}
else {
esyslog("ERROR: out of memory");
return false;
}
} }
return true;
} }
void cComponents::SetComponent(int Index, const char *s) void cComponents::SetComponent(int Index, const char *s)
{ {
Realloc(Index); if (Realloc(Index))
components[Index].FromString(s); components[Index].FromString(s);
} }
void cComponents::SetComponent(int Index, uchar Stream, uchar Type, const char *Language, const char *Description) void cComponents::SetComponent(int Index, uchar Stream, uchar Type, const char *Language, const char *Description)
{ {
Realloc(Index); if (!Realloc(Index))
return;
tComponent *p = &components[Index]; tComponent *p = &components[Index];
p->stream = Stream; p->stream = Stream;
p->type = Type; p->type = Type;

4
epg.h
View File

@ -7,7 +7,7 @@
* Original version (as used in VDR before 1.3.0) written by * Original version (as used in VDR before 1.3.0) written by
* Robert Schneider <Robert.Schneider@web.de> and Rolf Hakenes <hakenes@hippomi.de>. * Robert Schneider <Robert.Schneider@web.de> and Rolf Hakenes <hakenes@hippomi.de>.
* *
* $Id: epg.h 2.4 2010/01/08 15:20:34 kls Exp $ * $Id: epg.h 2.5 2011/02/25 14:14:38 kls Exp $
*/ */
#ifndef __EPG_H #ifndef __EPG_H
@ -51,7 +51,7 @@ class cComponents {
private: private:
int numComponents; int numComponents;
tComponent *components; tComponent *components;
void Realloc(int Index); bool Realloc(int Index);
public: public:
cComponents(void); cComponents(void);
~cComponents(void); ~cComponents(void);

12
menu.c
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: menu.c 2.25 2010/12/12 13:36:20 kls Exp $ * $Id: menu.c 2.26 2011/02/25 14:24:32 kls Exp $
*/ */
#include "menu.h" #include "menu.h"
@ -1860,8 +1860,14 @@ eOSState cMenuCommands::Execute(void)
int l = 0; int l = 0;
int c; int c;
while ((c = fgetc(p)) != EOF) { while ((c = fgetc(p)) != EOF) {
if (l % 20 == 0) if (l % 20 == 0) {
result = (char *)realloc(result, l + 21); if (char *NewBuffer = (char *)realloc(result, l + 21))
result = NewBuffer;
else {
esyslog("ERROR: out of memory");
break;
}
}
result[l++] = char(c); result[l++] = char(c);
} }
if (result) if (result)

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: recording.c 2.24 2010/12/27 12:02:00 kls Exp $ * $Id: recording.c 2.25 2011/02/25 14:35:19 kls Exp $
*/ */
#include "recording.h" #include "recording.h"
@ -548,13 +548,17 @@ char *ExchangeChars(char *s, bool ToFileSystem)
default: default:
if (strchr(InvalidChars, *p) || *p == '.' && (!*(p + 1) || *(p + 1) == FOLDERDELIMCHAR)) { // Windows can't handle '.' at the end of file/directory names if (strchr(InvalidChars, *p) || *p == '.' && (!*(p + 1) || *(p + 1) == FOLDERDELIMCHAR)) { // Windows can't handle '.' at the end of file/directory names
int l = p - s; int l = p - s;
s = (char *)realloc(s, strlen(s) + 10); if (char *NewBuffer = (char *)realloc(s, strlen(s) + 10)) {
p = s + l; s = NewBuffer;
char buf[4]; p = s + l;
sprintf(buf, "#%02X", (unsigned char)*p); char buf[4];
memmove(p + 2, p, strlen(p) + 1); sprintf(buf, "#%02X", (unsigned char)*p);
strncpy(p, buf, 3); memmove(p + 2, p, strlen(p) + 1);
p += 2; strncpy(p, buf, 3);
p += 2;
}
else
esyslog("ERROR: out of memory");
} }
} }
} }
@ -729,9 +733,13 @@ cRecording::cRecording(const char *FileName)
if (data[line]) { if (data[line]) {
int len = strlen(s); int len = strlen(s);
len += strlen(data[line]) + 1; len += strlen(data[line]) + 1;
data[line] = (char *)realloc(data[line], len + 1); if (char *NewBuffer = (char *)realloc(data[line], len + 1)) {
strcat(data[line], "\n"); data[line] = NewBuffer;
strcat(data[line], s); strcat(data[line], "\n");
strcat(data[line], s);
}
else
esyslog("ERROR: out of memory");
} }
else else
data[line] = strdup(s); data[line] = strdup(s);
@ -750,12 +758,16 @@ cRecording::cRecording(const char *FileName)
// line 1 and line 2 to be the long text: // line 1 and line 2 to be the long text:
int len = strlen(data[1]); int len = strlen(data[1]);
if (len > 80) { if (len > 80) {
data[1] = (char *)realloc(data[1], len + 1 + strlen(data[2]) + 1); if (char *NewBuffer = (char *)realloc(data[1], len + 1 + strlen(data[2]) + 1)) {
strcat(data[1], "\n"); data[1] = NewBuffer;
strcat(data[1], data[2]); strcat(data[1], "\n");
free(data[2]); strcat(data[1], data[2]);
data[2] = data[1]; free(data[2]);
data[1] = NULL; data[2] = data[1];
data[1] = NULL;
}
else
esyslog("ERROR: out of memory");
} }
} }
info->SetData(data[0], data[1], data[2]); info->SetData(data[0], data[1], data[2]);
@ -1620,13 +1632,15 @@ bool cIndexFile::CatchUp(int Index)
} }
int newLast = int(buf.st_size / sizeof(tIndexTs) - 1); int newLast = int(buf.st_size / sizeof(tIndexTs) - 1);
if (newLast > last) { if (newLast > last) {
if (size <= newLast) { int NewSize = size;
size *= 2; if (NewSize <= newLast) {
if (size <= newLast) NewSize *= 2;
size = newLast + 1; if (NewSize <= newLast)
NewSize = newLast + 1;
} }
index = (tIndexTs *)realloc(index, size * sizeof(tIndexTs)); if (tIndexTs *NewBuffer = (tIndexTs *)realloc(index, NewSize * sizeof(tIndexTs))) {
if (index) { size = NewSize;
index = NewBuffer;
int offset = (last + 1) * sizeof(tIndexTs); int offset = (last + 1) * sizeof(tIndexTs);
int delta = (newLast - last) * sizeof(tIndexTs); int delta = (newLast - last) * sizeof(tIndexTs);
if (lseek(f, offset, SEEK_SET) == offset) { if (lseek(f, offset, SEEK_SET) == offset) {
@ -1645,8 +1659,10 @@ bool cIndexFile::CatchUp(int Index)
else else
LOG_ERROR_STR(fileName); LOG_ERROR_STR(fileName);
} }
else else {
esyslog("ERROR: can't realloc() index"); esyslog("ERROR: can't realloc() index");
break;
}
} }
} }
else else

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: remux.c 2.49 2011/02/20 17:27:47 kls Exp $ * $Id: remux.c 2.50 2011/02/25 15:19:57 kls Exp $
*/ */
#include "remux.h" #include "remux.h"
@ -672,6 +672,7 @@ void cTsToPes::PutTs(const uchar *Data, int Length)
size = NewSize; size = NewSize;
} }
else { else {
esyslog("ERROR: out of memory");
Reset(); Reset();
return; return;
} }

14
svdrp.c
View File

@ -10,7 +10,7 @@
* and interact with the Video Disk Recorder - or write a full featured * and interact with the Video Disk Recorder - or write a full featured
* graphical interface that sits on top of an SVDRP connection. * graphical interface that sits on top of an SVDRP connection.
* *
* $Id: svdrp.c 2.8 2010/01/17 12:23:31 kls Exp $ * $Id: svdrp.c 2.9 2011/02/25 14:38:45 kls Exp $
*/ */
#include "svdrp.h" #include "svdrp.h"
@ -1664,8 +1664,16 @@ bool cSVDRP::Process(void)
} }
else { else {
if (numChars >= length - 1) { if (numChars >= length - 1) {
length += BUFSIZ; int NewLength = length + BUFSIZ;
cmdLine = (char *)realloc(cmdLine, length); if (char *NewBuffer = (char *)realloc(cmdLine, NewLength)) {
length = NewLength;
cmdLine = NewBuffer;
}
else {
esyslog("ERROR: out of memory");
Close();
break;
}
} }
cmdLine[numChars++] = c; cmdLine[numChars++] = c;
cmdLine[numChars] = 0; cmdLine[numChars] = 0;

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: themes.c 1.8 2008/02/10 16:25:00 kls Exp $ * $Id: themes.c 2.1 2011/02/25 14:45:18 kls Exp $
*/ */
#include "themes.h" #include "themes.h"
@ -248,12 +248,30 @@ bool cThemes::Load(const char *SkinName)
cString FileName = AddDirectory(themesDirectory, e->d_name); cString FileName = AddDirectory(themesDirectory, e->d_name);
cTheme Theme; cTheme Theme;
if (Theme.Load(*FileName, true)) { if (Theme.Load(*FileName, true)) {
names = (char **)realloc(names, (numThemes + 1) * sizeof(char *)); if (char **NewBuffer = (char **)realloc(names, (numThemes + 1) * sizeof(char *))) {
names[numThemes] = strdup(Theme.Name()); names = NewBuffer;
fileNames = (char **)realloc(fileNames, (numThemes + 1) * sizeof(char *)); names[numThemes] = strdup(Theme.Name());
fileNames[numThemes] = strdup(*FileName); }
descriptions = (char **)realloc(descriptions, (numThemes + 1) * sizeof(char *)); else {
descriptions[numThemes] = strdup(Theme.Description()); esyslog("ERROR: out of memory");
break;
}
if (char **NewBuffer = (char **)realloc(fileNames, (numThemes + 1) * sizeof(char *))) {
fileNames = NewBuffer;
fileNames[numThemes] = strdup(*FileName);
}
else {
esyslog("ERROR: out of memory");
break;
}
if (char **NewBuffer = (char **)realloc(descriptions, (numThemes + 1) * sizeof(char *))) {
descriptions = NewBuffer;
descriptions[numThemes] = strdup(Theme.Description());
}
else {
esyslog("ERROR: out of memory");
break;
}
numThemes++; numThemes++;
} }
} }

57
tools.c
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: tools.c 2.11 2010/12/12 23:15:38 kls Exp $ * $Id: tools.c 2.12 2011/02/25 14:58:31 kls Exp $
*/ */
#include "tools.h" #include "tools.h"
@ -156,8 +156,14 @@ char *strreplace(char *s, const char *s1, const char *s2)
int l = strlen(s); int l = strlen(s);
int l1 = strlen(s1); int l1 = strlen(s1);
int l2 = strlen(s2); int l2 = strlen(s2);
if (l2 > l1) if (l2 > l1) {
s = (char *)realloc(s, l + l2 - l1 + 1); if (char *NewBuffer = (char *)realloc(s, l + l2 - l1 + 1))
s = NewBuffer;
else {
esyslog("ERROR: out of memory");
return s;
}
}
char *sof = s + of; char *sof = s + of;
if (l2 != l1) if (l2 != l1)
memmove(sof + l2, sof + l1, l - of - l1 + 1); memmove(sof + l2, sof + l1, l - of - l1 + 1);
@ -819,8 +825,15 @@ const char *cCharSetConv::Convert(const char *From, char *To, size_t ToLength)
size_t FromLength = strlen(From); size_t FromLength = strlen(From);
char *ToPtr = To; char *ToPtr = To;
if (!ToPtr) { if (!ToPtr) {
length = max(length, FromLength * 2); // some reserve to avoid later reallocations int NewLength = max(length, FromLength * 2); // some reserve to avoid later reallocations
result = (char *)realloc(result, length); if (char *NewBuffer = (char *)realloc(result, NewLength)) {
length = NewLength;
result = NewBuffer;
}
else {
esyslog("ERROR: out of memory");
return From;
}
ToPtr = result; ToPtr = result;
ToLength = length; ToLength = length;
} }
@ -836,8 +849,15 @@ const char *cCharSetConv::Convert(const char *From, char *To, size_t ToLength)
// The result buffer is too small, so increase it: // The result buffer is too small, so increase it:
size_t d = ToPtr - result; size_t d = ToPtr - result;
size_t r = length / 2; size_t r = length / 2;
length += r; int NewLength = length + r;
Converted = result = (char *)realloc(result, length); if (char *NewBuffer = (char *)realloc(result, NewLength)) {
length = NewLength;
Converted = result = NewBuffer;
}
else {
esyslog("ERROR: out of memory");
return From;
}
ToLength += r; ToLength += r;
ToPtr = result + d; ToPtr = result + d;
} }
@ -1029,15 +1049,22 @@ static boolean JpegCompressEmptyOutputBuffer(j_compress_ptr cinfo)
tJpegCompressData *jcd = (tJpegCompressData *)cinfo->client_data; tJpegCompressData *jcd = (tJpegCompressData *)cinfo->client_data;
if (jcd) { if (jcd) {
int Used = jcd->size; int Used = jcd->size;
jcd->size += JPEGCOMPRESSMEM; int NewSize = jcd->size + JPEGCOMPRESSMEM;
jcd->mem = (uchar *)realloc(jcd->mem, jcd->size); if (uchar *NewBuffer = (uchar *)realloc(jcd->mem, NewSize)) {
jcd->size = NewSize;
jcd->mem = NewBuffer;
}
else {
esyslog("ERROR: out of memory");
return false;
}
if (jcd->mem) { if (jcd->mem) {
cinfo->dest->next_output_byte = jcd->mem + Used; cinfo->dest->next_output_byte = jcd->mem + Used;
cinfo->dest->free_in_buffer = jcd->size - Used; cinfo->dest->free_in_buffer = jcd->size - Used;
return TRUE; return true;
} }
} }
return FALSE; return false;
} }
static void JpegCompressTermDestination(j_compress_ptr cinfo) static void JpegCompressTermDestination(j_compress_ptr cinfo)
@ -1046,8 +1073,12 @@ static void JpegCompressTermDestination(j_compress_ptr cinfo)
if (jcd) { if (jcd) {
int Used = cinfo->dest->next_output_byte - jcd->mem; int Used = cinfo->dest->next_output_byte - jcd->mem;
if (Used < jcd->size) { if (Used < jcd->size) {
jcd->size = Used; if (uchar *NewBuffer = (uchar *)realloc(jcd->mem, Used)) {
jcd->mem = (uchar *)realloc(jcd->mem, jcd->size); jcd->size = Used;
jcd->mem = NewBuffer;
}
else
esyslog("ERROR: out of memory");
} }
} }
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: tools.h 2.6 2010/12/12 23:15:52 kls Exp $ * $Id: tools.h 2.7 2011/02/25 15:05:58 kls Exp $
*/ */
#ifndef __TOOLS_H #ifndef __TOOLS_H
@ -434,6 +434,10 @@ private:
{ {
if (++Index > allocated) { if (++Index > allocated) {
data = (T *)realloc(data, Index * sizeof(T)); data = (T *)realloc(data, Index * sizeof(T));
if (!data) {
esyslog("ERROR: out of memory - abort!");
abort();
}
for (int i = allocated; i < Index; i++) for (int i = allocated; i < Index; i++)
data[i] = T(0); data[i] = T(0);
allocated = Index; allocated = Index;