Merge "libsysutils: fix null pointer and memory leak issue"
This commit is contained in:
commit
51a2e4d5d4
1 changed files with 8 additions and 1 deletions
|
|
@ -112,6 +112,12 @@ char *SocketClient::quoteArg(const char *arg) {
|
||||||
char *result = (char *)malloc(len * 2 + 3);
|
char *result = (char *)malloc(len * 2 + 3);
|
||||||
char *current = result;
|
char *current = result;
|
||||||
const char *end = arg + len;
|
const char *end = arg + len;
|
||||||
|
char *oldresult;
|
||||||
|
|
||||||
|
if(result == NULL) {
|
||||||
|
SLOGW("malloc error (%s)", strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
*(current++) = '"';
|
*(current++) = '"';
|
||||||
while (arg < end) {
|
while (arg < end) {
|
||||||
|
|
@ -125,8 +131,9 @@ char *SocketClient::quoteArg(const char *arg) {
|
||||||
}
|
}
|
||||||
*(current++) = '"';
|
*(current++) = '"';
|
||||||
*(current++) = '\0';
|
*(current++) = '\0';
|
||||||
|
oldresult = result; // save pointer in case realloc fails
|
||||||
result = (char *)realloc(result, current-result);
|
result = (char *)realloc(result, current-result);
|
||||||
return result;
|
return result ? result : oldresult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue