ARTEMIS-2260 Fixes a potential NULL pointer dereference on the thin library

If the code fails to allocate native memory for the error message, it
will still perform the call to strcpy, which will result in a
segmentation fault to occur. This may cause the JVM to shutdown abruptly
potentially causing the original root cause to be hidden.
This commit is contained in:
Otavio Rodolfo Piske 2019-01-29 14:34:42 +01:00 committed by Clebert Suconic
parent 059e56eb6a
commit 584a610f6a
1 changed files with 6 additions and 4 deletions

View File

@ -113,10 +113,12 @@ char* exceptionMessage(char* msg, int error) {
error = error * -1; error = error * -1;
} }
//strerror is returning a constant, so no need to free anything coming from strerror //strerror is returning a constant, so no need to free anything coming from strerror
char* err = strerror(error); char *result = NULL;
char* result = malloc(strlen(msg) + strlen(err) + 1);
strcpy(result, msg); if (asprintf(&result, "%s%s", msg, strerror(error)) == -1) {
strcat(result, err); fprintf(stderr, "Could not allocate enough memory for the error message: %s/%s\n", msg, strerror(error));
}
return result; return result;
} }