Fix a memory leak
Static analyzer complaint:
frameworks/av/services/audiopolicy/service/AudioPolicyEffects.cpp:551:5:
warning: Potential leak of memory pointed to by 'fx_param'
[clang-analyzer-unix.Malloc]
This is because we unconditionally assign the result of realloc to
param: if realloc fails, it will not free the given pointer value, but
will return NULL. Hence, code like:
bool safeGrowParamSize(char **param, size_t size,
size_t *curSize, size_t *totSize) {
if (growParamSize(param, size, curSize, totSize) == 0) {
free(*param);
return false;
}
return true;
}
...Will fail to free param on failure.
Bug: None
Test: None
Change-Id: I7a430e7965ab29e183c82ebcb298a2ffb42339b1
diff --git a/services/audiopolicy/service/AudioPolicyEffects.cpp b/services/audiopolicy/service/AudioPolicyEffects.cpp
index 7fe363d..c7dfe0f 100644
--- a/services/audiopolicy/service/AudioPolicyEffects.cpp
+++ b/services/audiopolicy/service/AudioPolicyEffects.cpp
@@ -399,11 +399,12 @@
while (pos + size > *totSize) {
*totSize += ((*totSize + 7) / 8) * 4;
}
- *param = (char *)realloc(*param, *totSize);
- if (*param == NULL) {
+ char *newParam = (char *)realloc(*param, *totSize);
+ if (newParam == NULL) {
ALOGE("%s realloc error for size %zu", __func__, *totSize);
return 0;
}
+ *param = newParam;
}
*curSize = pos + size;
return pos;