mirror of https://github.com/xzeldon/htop.git
Release old memory on error
Avoids leaking memory upon realloc failure.
This commit is contained in:
parent
5e4b182616
commit
c138d14897
12
XUtils.c
12
XUtils.c
|
@ -13,6 +13,7 @@ in the source distribution for its full text.
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "CRT.h"
|
#include "CRT.h"
|
||||||
|
|
||||||
|
@ -20,6 +21,8 @@ in the source distribution for its full text.
|
||||||
void fail() {
|
void fail() {
|
||||||
CRT_done();
|
CRT_done();
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
|
_exit(1); // Should never reach here
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xMalloc(size_t size) {
|
void* xMalloc(size_t size) {
|
||||||
|
@ -39,8 +42,13 @@ void* xCalloc(size_t nmemb, size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xRealloc(void* ptr, size_t size) {
|
void* xRealloc(void* ptr, size_t size) {
|
||||||
void* data = realloc(ptr, size);
|
if (!size) {
|
||||||
if (!data && size > 0) {
|
free(ptr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
void* data = realloc(ptr, size); // deepcode ignore MemoryLeakOnRealloc: this goes to fail()
|
||||||
|
if (!data) {
|
||||||
|
free(ptr);
|
||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
|
|
Loading…
Reference in New Issue