Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3194 from Piankero/out-of-memory-error-template
Browse files Browse the repository at this point in the history
Out of memory error template. Approach #2
  • Loading branch information
markus2330 authored Nov 19, 2019
2 parents c204544 + 4a190e5 commit cc2a314
Show file tree
Hide file tree
Showing 26 changed files with 71 additions and 89 deletions.
1 change: 1 addition & 0 deletions doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ you up to date with the multi-language support provided by Elektra.
- Improved various error messages and synchronized documentations. _(Michael Zronek)_
- Improved `range` plugin error message. _(Michael Zronek)_
- Improved error codes documentation to clarify the hierarchy for developers. _(Michael Zronek)_
- Out of memory error messages are now uniform. _(Michael Zronek)_
- Release notes now use git's union merge driver. _(Dominic Jäger)_
- Please remove me. I'm only here for the build server. _(Dominic Jäger)_
- I'm only here for the build server. This PR contains only fix ups. _(Dominic Jäger)_
Expand Down
27 changes: 7 additions & 20 deletions src/include/kdberrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,20 @@ using KeySet = ckdb::KeySet;
__VA_ARGS__); \
} while (0)

#define ELEKTRA_SET_OUT_OF_MEMORY_ERROR(key, reason) \
#define ELEKTRA_SET_OUT_OF_MEMORY_ERROR(key) \
do \
{ \
ELEKTRA_LOG ("Add Error %s: %s", ELEKTRA_ERROR_OUT_OF_MEMORY, reason); \
ELEKTRA_LOG ("Add Error %s", ELEKTRA_ERROR_OUT_OF_MEMORY); \
elektraSetErrorOUT_OF_MEMORY (key, __FILE__, ELEKTRA_STRINGIFY (__LINE__), ELEKTRA_STRINGIFY (ELEKTRA_MODULE_NAME), \
reason); \
"Memory allocation failed"); \
} while (0)
#define ELEKTRA_SET_OUT_OF_MEMORY_ERRORF(key, reason, ...) \
do \
{ \
ELEKTRA_LOG ("Add Error %s: " reason, ELEKTRA_ERROR_OUT_OF_MEMORY, __VA_ARGS__); \
elektraSetErrorOUT_OF_MEMORY (key, __FILE__, ELEKTRA_STRINGIFY (__LINE__), ELEKTRA_STRINGIFY (ELEKTRA_MODULE_NAME), \
reason, __VA_ARGS__); \
} while (0)
#define ELEKTRA_ADD_OUT_OF_MEMORY_WARNING(key, reason) \
do \
{ \
ELEKTRA_LOG ("Add Warning %s: %s", ELEKTRA_WARNING_OUT_OF_MEMORY, reason); \
elektraAddWarningOUT_OF_MEMORY (key, __FILE__, ELEKTRA_STRINGIFY (__LINE__), ELEKTRA_STRINGIFY (ELEKTRA_MODULE_NAME), \
reason); \
} while (0)
#define ELEKTRA_ADD_OUT_OF_MEMORY_WARNINGF(key, reason, ...) \

#define ELEKTRA_ADD_OUT_OF_MEMORY_WARNING(key) \
do \
{ \
ELEKTRA_LOG ("Add Warning %s: " reason, ELEKTRA_WARNING_OUT_OF_MEMORY, __VA_ARGS__); \
ELEKTRA_LOG ("Add Warning %s: %s", ELEKTRA_WARNING_OUT_OF_MEMORY, "Memory allocation failed"); \
elektraAddWarningOUT_OF_MEMORY (key, __FILE__, ELEKTRA_STRINGIFY (__LINE__), ELEKTRA_STRINGIFY (ELEKTRA_MODULE_NAME), \
reason, __VA_ARGS__); \
"Memory allocation failed"); \
} while (0)

#define ELEKTRA_SET_INSTALLATION_ERROR(key, reason) \
Expand Down
2 changes: 0 additions & 2 deletions src/include/kdbmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
strerror (errno)); \
} while (0)

#define ELEKTRA_MALLOC_ERROR(key, size) ELEKTRA_SET_OUT_OF_MEMORY_ERRORF (key, "Unable to allocate %zu bytes", size);

/**
* @brief Sets error if info != returned
*
Expand Down
4 changes: 2 additions & 2 deletions src/libs/elektra/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void elektraTriggerWarnings (const char * nr, Key * parentKey, const char * mess
}
if (strcmp (nr, ELEKTRA_WARNING_OUT_OF_MEMORY) == 0)
{
ELEKTRA_ADD_OUT_OF_MEMORY_WARNING (parentKey, message);
ELEKTRA_ADD_OUT_OF_MEMORY_WARNING (parentKey);
return;
}
if (strcmp (nr, ELEKTRA_WARNING_INSTALLATION) == 0)
Expand Down Expand Up @@ -240,7 +240,7 @@ void elektraTriggerError (const char * nr, Key * parentKey, const char * message
}
if (strcmp (nr, ELEKTRA_ERROR_OUT_OF_MEMORY) == 0)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, message);
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
return;
}
if (strcmp (nr, ELEKTRA_ERROR_INSTALLATION) == 0)
Expand Down
6 changes: 3 additions & 3 deletions src/libs/merge/kdbmerge.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ static char * getValuesAsArray (KeySet * ks, const Key * arrayStart, Key * infor
}
if (elektraRealloc ((void **) &buffer, bufferSize) < 0)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (informationKey, "Memory allocation failed.");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (informationKey);
elektraFree (buffer);
keyDel (iterator);
return NULL;
Expand Down Expand Up @@ -776,14 +776,14 @@ static KeySet * ksFromArray (const char * array, int length, Key * informationKe
KeySet * result = ksNew (0, KS_END);
if (result == NULL)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (informationKey, "Memory allocation failed.");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (informationKey);
return NULL;
}
Key * iterator = keyNew ("/#0", KEY_END);
if (iterator == NULL)
{
ksDel (result);
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (informationKey, "Memory allocation failed.");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (informationKey);
return NULL;
}
char * buffer = elektraCalloc (length + 1); // + 1 for terminating null character
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/augeas/augeas.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ static int saveTree (augeas * augeasHandle, KeySet * ks, const char * lensPath,

memoryerror:
elektraFree (keyArray);
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, "Unable to allocate memory while saving the augeas tree");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
return -1;
}

Expand All @@ -432,7 +432,7 @@ int elektraAugeasOpen (Plugin * handle, Key * parentKey)

if (ret >= 0)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, "Unable to allocate memory for a detailed augeas error message");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
return -1;
}

Expand Down Expand Up @@ -521,7 +521,7 @@ int elektraAugeasGet (Plugin * handle, KeySet * returned, Key * parentKey)
if (!conversionData)
{
fclose (fh);
ELEKTRA_SET_OUT_OF_MEMORY_ERRORF (parentKey, "Out of memory. Errno: %s", strerror (errno));
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
}

conversionData->currentOrder = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/base64/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static int unescape (Key * key, Key * parent)
char * unescaped = elektraStrDup (&strVal[1]);
if (!unescaped)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent);
return -1;
}
keySetString (key, unescaped);
Expand Down Expand Up @@ -112,7 +112,7 @@ static int decode (Key * key, Key * parent, bool metaMode)
else if (result == -2)
{
// Memory error
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent);
return -1;
}

Expand All @@ -138,7 +138,7 @@ static int encode (Key * key, Key * parent, bool metaMode)
char * base64 = base64Encode (keyValue (key), (size_t) keyGetValueSize (key));
if (!base64)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent);
return -1;
}

Expand All @@ -152,7 +152,7 @@ static int encode (Key * key, Key * parent, bool metaMode)
char * newVal = elektraMalloc (newValLen);
if (!newVal)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent);
elektraFree (base64);
return -1;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ static int escape (Key * key, Key * parent)
char * escapedVal = elektraMalloc (strValLen + 2);
if (!escapedVal)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent);
return -1;
}

Expand Down
20 changes: 8 additions & 12 deletions src/plugins/conditionals/conditionals.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ static CondResult evalCondition (const Key * curKey, const char * leftSide, Comp
}
if (elektraRealloc ((void **) &compareTo, (size_t) (endPos - rightSide)) < 0)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, "Out of memory");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
result = ERROR;
goto Cleanup;
}
Expand All @@ -226,7 +226,7 @@ static CondResult evalCondition (const Key * curKey, const char * leftSide, Comp

if (elektraRealloc ((void **) &lookupName, (size_t) len) < 0)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, "Out of memory");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
result = ERROR;
goto Cleanup;
}
Expand All @@ -251,7 +251,7 @@ static CondResult evalCondition (const Key * curKey, const char * leftSide, Comp
}
if (elektraRealloc ((void **) &compareTo, (size_t) keyGetValueSize (key)) < 0)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, "Out of memory");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
result = ERROR;
goto Cleanup;
}
Expand All @@ -267,7 +267,7 @@ static CondResult evalCondition (const Key * curKey, const char * leftSide, Comp

if (elektraRealloc ((void **) &lookupName, (size_t) len) < 0)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, "Out of memory");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
result = ERROR;
goto Cleanup;
}
Expand Down Expand Up @@ -562,8 +562,7 @@ static CondResult parseCondition (Key * key, const char * condition, const Key *

if ((regcomp (&regex, regexString, REG_EXTENDED | REG_NEWLINE)))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey,
"Couldn't compile regex: most likely out of memory"); // the regex compiles so the only
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey); // the regex compiles so the only
// possible error would be out of
// memory
ksDel (ks);
Expand Down Expand Up @@ -617,17 +616,15 @@ static CondResult parseConditionString (const Key * meta, const Key * suffixList
CondResult ret;
if ((ret = regcomp (&regex1, regexString1, REGEX_FLAGS_CONDITION)))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey,
"Couldn't compile regex: most likely out of memory"); // the regex compiles so the only
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey); // the regex compiles so the only
// possible error would be out of
// memory
ksDel (ks);
return ERROR;
}
if ((ret = regcomp (&regex2, regexString2, REGEX_FLAGS_CONDITION)))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey,
"Couldn't compile regex: most likely out of memory"); // the regex compiles so the only
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey); // the regex compiles so the only
// possible error would be out of
// memory
regfree (&regex1);
Expand All @@ -636,8 +633,7 @@ static CondResult parseConditionString (const Key * meta, const Key * suffixList
}
if ((ret = regcomp (&regex3, regexString3, REGEX_FLAGS_CONDITION)))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey,
"Couldn't compile regex: most likely out of memory"); // the regex compiles so the only
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey); // the regex compiles so the only
// possible error would be out of
// memory
regfree (&regex1);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/crypto/botan_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static int getKeyIvForEncryption (KeySet * config, Key * errorKey, Key * masterK
}
if (!saltHexString)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}
keySetMeta (k, ELEKTRA_CRYPTO_META_SALT, saltHexString);
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/crypto/gcrypt_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static int getKeyIvForEncryption (KeySet * config, Key * errorKey, Key * masterK
}
if (!saltHexString)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}
keySetMeta (k, ELEKTRA_CRYPTO_META_SALT, saltHexString);
Expand Down Expand Up @@ -205,7 +205,7 @@ int elektraCryptoGcryHandleCreate (elektraCryptoHandle ** handle, KeySet * confi
memset (ivBuffer, 0, sizeof (ivBuffer));
keyDel (key);
keyDel (iv);
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}

Expand Down Expand Up @@ -293,7 +293,7 @@ int elektraCryptoGcryEncrypt (elektraCryptoHandle * handle, Key * k, Key * error
kdb_octet_t * output = elektraMalloc (outputLen);
if (!output)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
elektraFree (salt);
return -1;
}
Expand Down Expand Up @@ -373,7 +373,7 @@ int elektraCryptoGcryDecrypt (elektraCryptoHandle * handle, Key * k, Key * error
kdb_octet_t * output = elektraMalloc (payloadLen);
if (!output)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}

Expand Down Expand Up @@ -446,7 +446,7 @@ char * elektraCryptoGcryCreateRandomString (Key * errorKey, const kdb_unsigned_s
}
if (!encoded)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
}
return encoded;
}
12 changes: 6 additions & 6 deletions src/plugins/crypto/gpg.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static char * genGpgCandidate (Key * errorKey, char * dir, const char * file)
char * result = elektraMalloc (resultLen);
if (!result)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return NULL;
}
snprintf (result, resultLen, "%s/%s", dir, file);
Expand Down Expand Up @@ -146,7 +146,7 @@ static int searchPathForBin (Key * errorKey, const char * bin, char ** result)
char * path = elektraMalloc (envPathLen);
if (!path)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}
memcpy (path, envPath, envPathLen);
Expand Down Expand Up @@ -196,7 +196,7 @@ int ELEKTRA_PLUGIN_FUNCTION (gpgGetBinary) (char ** gpgBin, KeySet * conf, Key *
*gpgBin = elektraMalloc (configPathLen + 1);
if (!(*gpgBin))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}
strncpy (*gpgBin, configPath, configPathLen);
Expand Down Expand Up @@ -237,7 +237,7 @@ int ELEKTRA_PLUGIN_FUNCTION (gpgGetBinary) (char ** gpgBin, KeySet * conf, Key *
*gpgBin = elektraStrDup (ELEKTRA_CRYPTO_DEFAULT_GPG2_BIN);
if (!(*gpgBin))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}
return 1;
Expand All @@ -249,7 +249,7 @@ int ELEKTRA_PLUGIN_FUNCTION (gpgGetBinary) (char ** gpgBin, KeySet * conf, Key *
*gpgBin = elektraStrDup (ELEKTRA_CRYPTO_DEFAULT_GPG1_BIN);
if (!(*gpgBin))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}
return 1;
Expand Down Expand Up @@ -645,7 +645,7 @@ int ELEKTRA_PLUGIN_FUNCTION (gpgCall) (KeySet * conf, Key * errorKey, Key * msgK
// estimated maximum output size = 2 * input (including headers, etc.)
if (msgKey && !(buffer = elektraMalloc (bufferSize)))
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
closePipe (pipe_stdin);
closePipe (pipe_stdout);
closePipe (pipe_stderr);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/crypto/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int ELEKTRA_PLUGIN_FUNCTION (getSaltFromMetakey) (Key * errorKey, Key * k, kdb_o
}
else if (result == -2)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey, "Memory allocation failed");
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (errorKey);
return -1;
}
else if (result < -2)
Expand Down
Loading

0 comments on commit cc2a314

Please sign in to comment.