1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-19 04:20:27 +01:00

wrlib: Improvements to Netpbm memory usage on errors

This is actually taken from patch:
  wrlib: Added support for webp image

in which it is out of place as it is unrelated

Signed-off-by: Carlos R. Mafra <crmafra@gmail.com>
This commit is contained in:
David Maciejak
2014-04-16 22:29:45 +02:00
committed by Carlos R. Mafra
parent 5c96c69cb5
commit 051a30f550

View File

@@ -112,12 +112,16 @@ static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
unsigned char *ptr; unsigned char *ptr;
int x, y; int x, y;
image = RCreateImage(w, h, 0); if (raw != '2' && raw != '5') {
if (!image) RErrorCode = RERR_BADFORMAT;
return NULL; return NULL;
}
if (raw != '2' && raw != '5') image = RCreateImage(w, h, 0);
return image; if (!image) {
RErrorCode = RERR_NOMEMORY;
return NULL;
}
if (max < 256) { if (max < 256) {
ptr = image->data; ptr = image->data;
@@ -129,6 +133,7 @@ static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
if (val > max || val < 0) { if (val > max || val < 0) {
RErrorCode = RERR_BADIMAGEFILE; RErrorCode = RERR_BADIMAGEFILE;
RReleaseImage(image);
return NULL; return NULL;
} }
@@ -142,12 +147,16 @@ static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
if (raw == '5') { if (raw == '5') {
char *buf; char *buf;
buf = malloc(w + 1); buf = malloc(w + 1);
if (!buf) if (!buf) {
RErrorCode = RERR_NOMEMORY;
RReleaseImage(image);
return NULL; return NULL;
}
for (y = 0; y < h; y++) { for (y = 0; y < h; y++) {
if (!fread(buf, w, 1, file)) { if (!fread(buf, w, 1, file)) {
free(buf); free(buf);
RErrorCode = RERR_BADIMAGEFILE; RErrorCode = RERR_BADIMAGEFILE;
RReleaseImage(image);
return NULL; return NULL;
} }
@@ -171,12 +180,16 @@ static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
unsigned char *ptr; unsigned char *ptr;
int i = 0; int i = 0;
image = RCreateImage(w, h, 0); if (raw != '3' && raw != '6') {
if (!image) RErrorCode = RERR_BADFORMAT;
return NULL; return NULL;
}
if (raw != '3' && raw != '6') image = RCreateImage(w, h, 0);
return image; if (!image) {
RErrorCode = RERR_NOMEMORY;
return NULL;
}
ptr = image->data; ptr = image->data;
if (max < 256) { if (max < 256) {
@@ -189,6 +202,7 @@ static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
if (val > max || val < 0) { if (val > max || val < 0) {
RErrorCode = RERR_BADIMAGEFILE; RErrorCode = RERR_BADIMAGEFILE;
RReleaseImage(image);
return NULL; return NULL;
} }
@@ -202,6 +216,7 @@ static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
while (i < w * h) { while (i < w * h) {
if (fread(buf, 1, 3, file) != 3) { if (fread(buf, 1, 3, file) != 3) {
RErrorCode = RERR_BADIMAGEFILE; RErrorCode = RERR_BADIMAGEFILE;
RReleaseImage(image);
return NULL; return NULL;
} }
@@ -222,12 +237,16 @@ static RImage *load_bitmap(FILE * file, int w, int h, int max, int raw)
int val; int val;
unsigned char *ptr; unsigned char *ptr;
image = RCreateImage(w, h, 0); if (raw != '1' && raw != '4') {
if (!image) RErrorCode = RERR_BADFORMAT;
return NULL; return NULL;
}
if (raw != '1' && raw != '4') image = RCreateImage(w, h, 0);
return image; if (!image) {
RErrorCode = RERR_NOMEMORY;
return NULL;
}
ptr = image->data; ptr = image->data;
if (raw == '1') { if (raw == '1') {
@@ -237,6 +256,7 @@ static RImage *load_bitmap(FILE * file, int w, int h, int max, int raw)
if (val > max || val < 0) { if (val > max || val < 0) {
RErrorCode = RERR_BADIMAGEFILE; RErrorCode = RERR_BADIMAGEFILE;
RReleaseImage(image);
return NULL; return NULL;
} }