diff options
author | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-06-24 14:45:41 +0200 |
---|---|---|
committer | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-06-25 11:15:43 +0200 |
commit | bdec00ada0634d039b91ec6d5b9793be2b68823c (patch) | |
tree | d460bb7284e32d8167ce39a8dc0654d47c0f99ad | |
parent | 70c11ed3c3f1f7ce80af6fd3997a8bb8a38d392e (diff) |
Rework json strings allocation/freeing
Rework json strings allocation/freeing because :
- Some json strings are freed twice.
- Some containing strings json are not correctly freed.
BUG-AGL: SPEC-2329
Change-Id: I90f9470c6a4bc4b1641a01dd2da5051da1ff9547
Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
-rw-r--r-- | lib/4a-hal-utilities/4a-hal-utilities-data.c | 118 | ||||
-rw-r--r-- | src/4a-internals-hal/4a-internals-hal-alsacore-link.c | 15 | ||||
-rw-r--r-- | src/4a-internals-hal/4a-internals-hal-cb.c | 80 |
3 files changed, 159 insertions, 54 deletions
diff --git a/lib/4a-hal-utilities/4a-hal-utilities-data.c b/lib/4a-hal-utilities/4a-hal-utilities-data.c index 3ac2a7c..6121b33 100644 --- a/lib/4a-hal-utilities/4a-hal-utilities-data.c +++ b/lib/4a-hal-utilities/4a-hal-utilities-data.c @@ -238,6 +238,24 @@ int HalUtlIsDependencySelected(struct InternalHalProbedDevice *probedDevice, enu } } +void HalUtlFreeSelectedProbedDeviceDataAllocation(struct InternalHalDeviceData *probedDeviceDataToFree) +{ + if(! probedDeviceDataToFree) + return; + + free(probedDeviceDataToFree->extendedCardNb); + free(probedDeviceDataToFree->cardId); + free(probedDeviceDataToFree->cardShortName); + free(probedDeviceDataToFree->cardLongName); + free(probedDeviceDataToFree->cardDriver); + free(probedDeviceDataToFree->cardMixerName); + free(probedDeviceDataToFree->cardComponents); + free(probedDeviceDataToFree->playbackDeviceId); + free(probedDeviceDataToFree->playbackDeviceName); + + free(probedDeviceDataToFree); +} + void HalUtlFreeSelectedProbedDeviceAllocation(struct InternalHalProbedDevice *probedDeviceToFree) { if(! probedDeviceToFree) @@ -245,19 +263,7 @@ void HalUtlFreeSelectedProbedDeviceAllocation(struct InternalHalProbedDevice *pr free(probedDeviceToFree->uid); - if(probedDeviceToFree->deviceData) { - free(probedDeviceToFree->deviceData->extendedCardNb); - free(probedDeviceToFree->deviceData->cardId); - free(probedDeviceToFree->deviceData->cardShortName); - free(probedDeviceToFree->deviceData->cardLongName); - free(probedDeviceToFree->deviceData->cardDriver); - free(probedDeviceToFree->deviceData->cardMixerName); - free(probedDeviceToFree->deviceData->cardComponents); - free(probedDeviceToFree->deviceData->playbackDeviceId); - free(probedDeviceToFree->deviceData->playbackDeviceName); - - free(probedDeviceToFree->deviceData); - } + HalUtlFreeSelectedProbedDeviceDataAllocation(probedDeviceToFree->deviceData); if(probedDeviceToFree->requestedDeviceJ) json_object_put(probedDeviceToFree->requestedDeviceJ); @@ -318,6 +324,15 @@ struct InternalHalDeviceData *HalUtlAllocateAndFillProbedDeviceDataUsingInfoGetR { struct InternalHalDeviceData *currentProbedDeviceData; + char *cardId, + *cardShortName, + *cardLongName, + *cardDriver, + *cardMixerName, + *cardComponents, + *playbackDeviceId = NULL, + *playbackDeviceName = NULL; + if(! responseJ) return NULL; @@ -330,15 +345,15 @@ struct InternalHalDeviceData *HalUtlAllocateAndFillProbedDeviceDataUsingInfoGetR if(wrap_json_unpack(responseJ, "{s:i, s:s, s:s, s:s, s:s, s:s, s:s, s?:i, s?:s, s?:s !}", "cardNb", ¤tProbedDeviceData->cardNb, - "cardId", ¤tProbedDeviceData->cardId, - "cardShortName", ¤tProbedDeviceData->cardShortName, - "cardLongName", ¤tProbedDeviceData->cardLongName, - "cardDriver", ¤tProbedDeviceData->cardDriver, - "cardMixerName", ¤tProbedDeviceData->cardMixerName, - "cardComponents", ¤tProbedDeviceData->cardComponents, + "cardId", &cardId, + "cardShortName", &cardShortName, + "cardLongName", &cardLongName, + "cardDriver", &cardDriver, + "cardMixerName", &cardMixerName, + "cardComponents", &cardComponents, "playbackDeviceNb", ¤tProbedDeviceData->playbackDeviceNb, - "playbackDeviceId", ¤tProbedDeviceData->playbackDeviceId, - "playbackDeviceName", ¤tProbedDeviceData->playbackDeviceName) || + "playbackDeviceId", &playbackDeviceId, + "playbackDeviceName", &playbackDeviceName) || currentProbedDeviceData->cardNb == HAL_UNKNOWN_DEVICE || (currentProbedDeviceData->playbackDeviceNb == HAL_UNKNOWN_DEVICE && asprintf(¤tProbedDeviceData->extendedCardNb, "hw:%i", currentProbedDeviceData->cardNb) < 0) || @@ -346,10 +361,62 @@ struct InternalHalDeviceData *HalUtlAllocateAndFillProbedDeviceDataUsingInfoGetR asprintf(¤tProbedDeviceData->extendedCardNb, "hw:%i,%i", currentProbedDeviceData->cardNb, currentProbedDeviceData->playbackDeviceNb) < 0)) { - free(currentProbedDeviceData); + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + + currentProbedDeviceData->cardId = strdup(cardId); + if(! currentProbedDeviceData->cardId) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + + currentProbedDeviceData->cardShortName = strdup(cardShortName); + if(! currentProbedDeviceData->cardShortName) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + + currentProbedDeviceData->cardLongName = strdup(cardLongName); + if(! currentProbedDeviceData->cardLongName) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); return NULL; } + currentProbedDeviceData->cardDriver = strdup(cardDriver); + if(! currentProbedDeviceData->cardDriver) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + + currentProbedDeviceData->cardMixerName = strdup(cardMixerName); + if(! currentProbedDeviceData->cardMixerName) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + + currentProbedDeviceData->cardComponents = strdup(cardComponents); + if(! currentProbedDeviceData->cardComponents) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + + if(playbackDeviceId) { + currentProbedDeviceData->playbackDeviceId = strdup(playbackDeviceId); + if(! currentProbedDeviceData->playbackDeviceId) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + } + + if(playbackDeviceName) { + currentProbedDeviceData->playbackDeviceName = strdup(playbackDeviceName); + if(! currentProbedDeviceData->playbackDeviceName) { + HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData); + return NULL; + } + } + return currentProbedDeviceData; } @@ -782,13 +849,6 @@ void HalUtlFreeSelectedHalAllocation(struct HalData *halDataToFree) if(! halDataToFree) return; - free(halDataToFree->apiName); - free(halDataToFree->sndCardPath); - free(halDataToFree->info); - free(halDataToFree->author); - free(halDataToFree->version); - free(halDataToFree->date); - if(halDataToFree->internal) { free(halDataToFree->internalHalData->mixerApiName); free(halDataToFree->internalHalData->prefix); diff --git a/src/4a-internals-hal/4a-internals-hal-alsacore-link.c b/src/4a-internals-hal/4a-internals-hal-alsacore-link.c index 902e5ad..e1ad6dd 100644 --- a/src/4a-internals-hal/4a-internals-hal-alsacore-link.c +++ b/src/4a-internals-hal/4a-internals-hal-alsacore-link.c @@ -287,7 +287,9 @@ int InternalHalUpdateAlsaCtlProperties(afb_api_t apiHandle, char *cardId, struct { int err = 0; - json_object *returnedDataJ; + char *name; + + json_object *returnedDataJ = NULL; err = InternalHalGetAlsaCtlInfo(apiHandle, cardId, currentAlsaCtl, &returnedDataJ); if(err) { @@ -295,7 +297,9 @@ int InternalHalUpdateAlsaCtlProperties(afb_api_t apiHandle, char *cardId, struct } // TBD JAI : get dblinear/dbminmax/... values else if(wrap_json_unpack(returnedDataJ, - "{s:{s?:i s?:i s?:i s?:i s?:i}}", + "{s:i s:s s:{s?:i s?:i s?:i s?:i s?:i}}", + "id", ¤tAlsaCtl->numid, + "name", &name, "ctl", "type", (int *) ¤tAlsaCtl->alsaCtlProperties.type, "count", ¤tAlsaCtl->alsaCtlProperties.count, @@ -310,6 +314,13 @@ int InternalHalUpdateAlsaCtlProperties(afb_api_t apiHandle, char *cardId, struct err = -9; } + else { + currentAlsaCtl->name = strdup(name); + if(! currentAlsaCtl->name) { + AFB_API_ERROR(apiHandle, "Didn't succeed to store (allocate) control 'name' string"); + err = -10; + } + } if(returnedDataJ) json_object_put(returnedDataJ); diff --git a/src/4a-internals-hal/4a-internals-hal-cb.c b/src/4a-internals-hal/4a-internals-hal-cb.c index a8d6dc3..1749131 100644 --- a/src/4a-internals-hal/4a-internals-hal-cb.c +++ b/src/4a-internals-hal/4a-internals-hal-cb.c @@ -149,7 +149,9 @@ void InternalHalDispatchApiEvent(afb_api_t apiHandle, const char *evtLabel, json int InternalHalHalMixerConfig(afb_api_t apiHandle, CtlSectionT *section, json_object *MixerJ) { - int err = 0; + int err = 0, wrapRet; + + char *mixerApiName, *prefix = NULL; CtlConfigT *ctrlConfig; struct HalData *currentHalData; @@ -171,16 +173,25 @@ int InternalHalHalMixerConfig(afb_api_t apiHandle, CtlSectionT *section, json_ob else return -4; - if(wrap_json_unpack(MixerJ, "{s:s}", "mixerapi", ¤tHalData->internalHalData->mixerApiName)) + wrapRet = wrap_json_unpack(MixerJ, "{s:s s?:s}", "mixerapi", &mixerApiName, "prefix", &prefix); + if(wrapRet) return -5; - wrap_json_unpack(MixerJ, "{s?:s}", "prefix", ¤tHalData->internalHalData->prefix); + currentHalData->internalHalData->mixerApiName = strdup(mixerApiName); + if(! currentHalData->internalHalData->mixerApiName) + return -6; + + if(prefix) { + currentHalData->internalHalData->prefix = strdup(prefix); + if(! currentHalData->internalHalData->prefix) + return -7; + } } else if(currentHalData->status == HAL_STATUS_AVAILABLE) { err = InternalHalAttachToMixer(apiHandle); if(err) { AFB_API_ERROR(apiHandle, "Error %i while attaching to mixer", err); - return -6; + return -8; } } @@ -193,28 +204,50 @@ int InternalHalHalMixerConfig(afb_api_t apiHandle, CtlSectionT *section, json_ob int InternalHalProcessOneHalMapObject(afb_api_t apiHandle, struct InternalHalAlsaMap *alsaMap, json_object *AlsaMapJ) { - char *action = NULL, *typename = NULL; + char *uid, *info = NULL, *action = NULL, *name = NULL, *typename = NULL; json_object *alsaJ = NULL, *createAlsaCtlJ = NULL; AFB_API_DEBUG(apiHandle, "AlsaMapJ=%s", json_object_get_string(AlsaMapJ)); if(wrap_json_unpack(AlsaMapJ, "{s:s s?:s s:o s?:s !}", - "uid", &alsaMap->uid, - "info", &alsaMap->info, + "uid", &uid, + "info", &info, "alsa", &alsaJ, "action", &action)) { AFB_API_ERROR(apiHandle, "Parsing error, map should only contains [label]|[uid]|[tag]|[info]|[alsa]|[action] in : '%s'", json_object_get_string(AlsaMapJ)); return -1; } + alsaMap->uid = strdup(uid); + if(! alsaMap->uid) { + AFB_API_ERROR(apiHandle, "Didn't succeed to store (allocate) control 'uid' string"); + return -2; + } + + if(info) { + alsaMap->info = strdup(info); + if(! alsaMap->info) { + AFB_API_ERROR(apiHandle, "Didn't succeed to store (allocate) control 'info' string"); + return -3; + } + } + if(wrap_json_unpack(alsaJ, "{s?:s s?:i s?:i s?:o !}", - "name", &alsaMap->ctl.name, + "name", &name, "numid", &alsaMap->ctl.numid, "value", &alsaMap->ctl.value, "create", &createAlsaCtlJ)) { AFB_API_ERROR(apiHandle, "Parsing error, alsa json should only contains [name]|[numid]||[value]|[create] in : '%s'", json_object_get_string(alsaJ)); - return -2; + return -4; + } + + if(name) { + alsaMap->ctl.name = strdup(name); + if(! alsaMap->ctl.name) { + AFB_API_ERROR(apiHandle, "Didn't succeed to store (allocate) control 'info' string"); + return -5; + } } if(createAlsaCtlJ) { @@ -228,7 +261,7 @@ int InternalHalProcessOneHalMapObject(afb_api_t apiHandle, struct InternalHalAls "maxval", &alsaMap->ctl.alsaCtlCreation->maxval, "step", &alsaMap->ctl.alsaCtlCreation->step)) { AFB_API_ERROR(apiHandle, "Parsing error, alsa creation json should only contains [type]|[count]|[minval]|[maxval]|[step] in : '%s'", json_object_get_string(alsaJ)); - return -3; + return -6; } if(typename) { @@ -238,7 +271,7 @@ int InternalHalProcessOneHalMapObject(afb_api_t apiHandle, struct InternalHalAls "Couldn't get alsa type from string %s in : '%s'", typename, json_object_get_string(alsaJ)); - return -4; + return -7; } } @@ -251,17 +284,17 @@ int InternalHalProcessOneHalMapObject(afb_api_t apiHandle, struct InternalHalAls alsaMap->ctl.name, alsaMap->ctl.numid, json_object_get_string(alsaJ)); - return -5; + return -8; } else if(! alsaMap->ctl.name && alsaMap->ctl.numid <= 0) { AFB_API_ERROR(apiHandle, "Need at least a control name or a control uid in alsa object : '%s'", json_object_get_string(alsaJ)); - return -6; + return -9; } if(action) - alsaMap->actionJ = AlsaMapJ; + alsaMap->actionJ = json_object_get(AlsaMapJ); return 0; } @@ -875,11 +908,11 @@ int InternalHalHandleInfoGetResponse(afb_api_t apiHandle, json_object *currentRe int InternalHalHandleOneHalDependencies(afb_api_t apiHandle, struct InternalHalProbedDevice **halDeviceToProbe) { - int ret; + int ret, toReturn = 0; struct InternalHalProbedDevice *currentDeviceToProbe; - json_object *probedDeviceGetInfoResponseJ; + json_object *probedDeviceGetInfoResponseJ = NULL; if(! apiHandle || ! halDeviceToProbe || ! *halDeviceToProbe) { AFB_API_ERROR(apiHandle, "Invalid argument(s)"); @@ -916,16 +949,15 @@ int InternalHalHandleOneHalDependencies(afb_api_t apiHandle, struct InternalHalP ret, currentDeviceToProbe->uid, json_object_get_string(probedDeviceGetInfoResponseJ)); - return -5; + toReturn = -5; } - - if(ret > 0) { + else if(ret > 0) { AFB_API_WARNING(apiHandle, "Multiple card was found for dependency with uid '%s'(request : '%s', response : '%s')", currentDeviceToProbe->uid, json_object_get_string(currentDeviceToProbe->requestedDeviceJ), json_object_get_string(probedDeviceGetInfoResponseJ)); - return 1; + toReturn = 1; } else if(! currentDeviceToProbe->deviceData) { AFB_API_INFO(apiHandle, @@ -933,10 +965,13 @@ int InternalHalHandleOneHalDependencies(afb_api_t apiHandle, struct InternalHalP currentDeviceToProbe->uid, json_object_get_string(currentDeviceToProbe->requestedDeviceJ), json_object_get_string(probedDeviceGetInfoResponseJ)); - return 2; + toReturn = 2; } - return 0; + if(probedDeviceGetInfoResponseJ) + json_object_put(probedDeviceGetInfoResponseJ); + + return toReturn; } int InternalHalHandleHalDependencies(afb_api_t apiHandle, @@ -982,7 +1017,6 @@ int InternalHalHandleHalDependencies(afb_api_t apiHandle, return -3; } - // If no uid is specified, handle all dependencies if(validatedDepedency) { ret = InternalHalHandleOneHalDependencies(apiHandle, &halCurrentDeviceToProbe); if(ret < 0) { |