From 5817fd4ac5308fe7c23301c652f174997009b7d5 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 4 Dec 2019 11:17:21 +0100 Subject: parse: simplify error paths in xpmParseColors() We introduced a new label to handle the errors, we should use it for the rest of the function. Signed-off-by: Benjamin Tissoires --- src/parse.c | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/parse.c b/src/parse.c index aa0fff7..613529e 100644 --- a/src/parse.c +++ b/src/parse.c @@ -246,19 +246,19 @@ xpmParseColors( * read pixel value */ if (cpp >= UINT_MAX - 1) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmNoMemory); + ErrorStatus = XpmNoMemory; + goto error; } color->string = (char *) XpmMalloc(cpp + 1); if (!color->string) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmNoMemory); + ErrorStatus = XpmNoMemory; + goto error; } for (b = 0, s = color->string; b < cpp; b++, s++) { int c = xpmGetC(data); if (c < 0) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmFileInvalid); + ErrorStatus = XpmFileInvalid; + goto error; } *s = (char) c; } @@ -271,8 +271,7 @@ xpmParseColors( ErrorStatus = xpmHashIntern(hashtable, color->string, HashAtomData(a)); if (ErrorStatus != XpmSuccess) { - xpmFreeColorTable(colorTable, ncolors); - return (ErrorStatus); + goto error; } } @@ -295,8 +294,8 @@ xpmParseColors( len = strlen(curbuf) + 1; s = (char *) XpmMalloc(len); if (!s) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmNoMemory); + ErrorStatus = XpmNoMemory; + goto error; } defaults[curkey] = s; memcpy(s, curbuf, len); @@ -306,8 +305,8 @@ xpmParseColors( lastwaskey = 1; } else { if (!curkey) { /* key without value */ - xpmFreeColorTable(colorTable, ncolors); - return (XpmFileInvalid); + ErrorStatus = XpmFileInvalid; + goto error; } if (!lastwaskey) { if (!xstrlcat(curbuf, " ", sizeof(curbuf))) { /* append space */ @@ -324,14 +323,14 @@ xpmParseColors( } } if (!curkey) { /* key without value */ - xpmFreeColorTable(colorTable, ncolors); - return (XpmFileInvalid); + ErrorStatus = XpmFileInvalid; + goto error; } len = strlen(curbuf) + 1; /* integer overflow just theoretically possible */ s = defaults[curkey] = (char *) XpmMalloc(len); if (!s) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmNoMemory); + ErrorStatus = XpmNoMemory; + goto error; } memcpy(s, curbuf, len); } @@ -347,19 +346,19 @@ xpmParseColors( * read pixel value */ if (cpp >= UINT_MAX - 1) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmNoMemory); + ErrorStatus = XpmNoMemory; + goto error; } color->string = (char *) XpmMalloc(cpp + 1); if (!color->string) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmNoMemory); + ErrorStatus = XpmNoMemory; + goto error; } for (b = 0, s = color->string; b < cpp; b++, s++) { int c = xpmGetC(data); if (c < 0) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmFileInvalid); + ErrorStatus = XpmFileInvalid; + goto error; } *s = (char) c; } @@ -372,8 +371,7 @@ xpmParseColors( ErrorStatus = xpmHashIntern(hashtable, color->string, HashAtomData(a)); if (ErrorStatus != XpmSuccess) { - xpmFreeColorTable(colorTable, ncolors); - return (ErrorStatus); + goto error; } } @@ -398,8 +396,8 @@ xpmParseColors( len = strlen(curbuf) + 1; s = (char *) XpmMalloc(len); if (!s) { - xpmFreeColorTable(colorTable, ncolors); - return (XpmNoMemory); + ErrorStatus = XpmNoMemory; + goto error; } memcpy(s, curbuf, len); color->c_color = s; -- cgit v1.2.3