From f43a8894cb7e30d46f49aedd0e6a7f1b97e19b6f Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 27 Aug 2025 10:33:57 +0800 Subject: [PATCH 1/2] string to num review fix Signed-off-by: Charles --- .../node/src/hpae_node_common.cpp | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/services/audio_engine/node/src/hpae_node_common.cpp b/services/audio_engine/node/src/hpae_node_common.cpp index c0ed5d1e3f..39e864637f 100644 --- a/services/audio_engine/node/src/hpae_node_common.cpp +++ b/services/audio_engine/node/src/hpae_node_common.cpp @@ -30,6 +30,7 @@ static constexpr uint32_t DEFAULT_MULTICHANNEL_CHANNELLAYOUT = 1551; static constexpr float MAX_SINK_VOLUME_LEVEL = 1.0; static constexpr uint32_t DEFAULT_MULTICHANNEL_FRAME_LEN_MS = 20; static constexpr uint32_t MS_PER_SECOND = 1000; +static constexpr uint32_t BASE_TEN = 10; static std::map g_streamTypeToSceneTypeMap = { {STREAM_MUSIC, HPAE_SCENE_MUSIC}, @@ -128,6 +129,14 @@ static std::unordered_map g_deviceClassToPipeMap = { {"multichannel", PIPE_TYPE_MULTICHANNEL}, }; +static long StringToNum(const std::string &str) +{ + char *endptr; + long num = strtol(str.c_str(), &endptr, BASE_TEN); + CHECK_AND_RETURN_RET_LOG(*endptr == '\0', 0, "trans str \"%{public}s\" to num failed", str.c_str()); + return num; +} + AudioPipeType ConvertDeviceClassToPipe(const std::string &deviceClass) { auto item = g_deviceClassToPipeMap.find(deviceClass); @@ -278,7 +287,7 @@ AudioSampleFormat TransFormatFromStringToEnum(std::string format) void AdjustMchSinkInfo(const AudioModuleInfo &audioModuleInfo, HpaeSinkInfo &sinkInfo) { if (sinkInfo.deviceName == "DP_MCH_speaker") { - sinkInfo.channelLayout = static_cast(std::atol(audioModuleInfo.channelLayout.c_str())); + sinkInfo.channelLayout = static_cast(StringToNum(audioModuleInfo.channelLayout)); return; } if (sinkInfo.deviceName != "MCH_Speaker") { @@ -314,20 +323,20 @@ int32_t TransModuleInfoToHpaeSinkInfo(const AudioModuleInfo &audioModuleInfo, Hp sinkInfo.splitMode = audioModuleInfo.extra; sinkInfo.filePath = audioModuleInfo.fileName; - sinkInfo.samplingRate = static_cast(std::atol(audioModuleInfo.rate.c_str())); + sinkInfo.samplingRate = static_cast(StringToNum(audioModuleInfo.rate)); sinkInfo.format = static_cast(TransFormatFromStringToEnum(audioModuleInfo.format)); - sinkInfo.channels = static_cast(std::atol(audioModuleInfo.channels.c_str())); - int32_t bufferSize = static_cast(std::atol(audioModuleInfo.bufferSize.c_str())); + sinkInfo.channels = static_cast(StringToNum(audioModuleInfo.channels)); + int32_t bufferSize = static_cast(StringToNum(audioModuleInfo.bufferSize)); sinkInfo.frameLen = static_cast(bufferSize) / (sinkInfo.channels * static_cast(GetSizeFromFormat(sinkInfo.format))); sinkInfo.channelLayout = 0ULL; - sinkInfo.deviceType = static_cast(std::atol(audioModuleInfo.deviceType.c_str())); + sinkInfo.deviceType = static_cast(StringToNum(audioModuleInfo.deviceType)); sinkInfo.volume = MAX_SINK_VOLUME_LEVEL; - sinkInfo.openMicSpeaker = static_cast(std::atol(audioModuleInfo.OpenMicSpeaker.c_str())); - sinkInfo.renderInIdleState = static_cast(std::atol(audioModuleInfo.renderInIdleState.c_str())); - sinkInfo.offloadEnable = static_cast(std::atol(audioModuleInfo.offloadEnable.c_str())); - sinkInfo.sinkLatency = static_cast(std::atol(audioModuleInfo.sinkLatency.c_str())); - sinkInfo.fixedLatency = static_cast(std::atol(audioModuleInfo.fixedLatency.c_str())); + sinkInfo.openMicSpeaker = static_cast(StringToNum(audioModuleInfo.OpenMicSpeaker)); + sinkInfo.renderInIdleState = static_cast(StringToNum(audioModuleInfo.renderInIdleState)); + sinkInfo.offloadEnable = static_cast(StringToNum(audioModuleInfo.offloadEnable)); + sinkInfo.sinkLatency = static_cast(StringToNum(audioModuleInfo.sinkLatency)); + sinkInfo.fixedLatency = static_cast(StringToNum(audioModuleInfo.fixedLatency)); sinkInfo.deviceName = audioModuleInfo.name; AdjustMchSinkInfo(audioModuleInfo, sinkInfo); if (audioModuleInfo.needEmptyChunk) { @@ -347,30 +356,30 @@ int32_t TransModuleInfoToHpaeSourceInfo(const AudioModuleInfo &audioModuleInfo, sourceInfo.adapterName = audioModuleInfo.adapterName; sourceInfo.sourceName = audioModuleInfo.name; // built_in_mic sourceInfo.deviceName = audioModuleInfo.name; - sourceInfo.sourceType = static_cast(std::atol(audioModuleInfo.sourceType.c_str())); + sourceInfo.sourceType = static_cast(StringToNum(audioModuleInfo.sourceType)); sourceInfo.filePath = audioModuleInfo.fileName; - int32_t bufferSize = static_cast(std::atol(audioModuleInfo.bufferSize.c_str())); - sourceInfo.channels = static_cast(std::atol(audioModuleInfo.channels.c_str())); + int32_t bufferSize = static_cast(StringToNum(audioModuleInfo.bufferSize)); + sourceInfo.channels = static_cast(StringToNum(audioModuleInfo.channels)); sourceInfo.format = TransFormatFromStringToEnum(audioModuleInfo.format); sourceInfo.frameLen = static_cast(bufferSize) / (sourceInfo.channels * static_cast(GetSizeFromFormat(sourceInfo.format))); - sourceInfo.samplingRate = static_cast(std::atol(audioModuleInfo.rate.c_str())); + sourceInfo.samplingRate = static_cast(StringToNum(audioModuleInfo.rate)); sourceInfo.channelLayout = 0ULL; - sourceInfo.deviceType = static_cast(std::atol(audioModuleInfo.deviceType.c_str())); + sourceInfo.deviceType = static_cast(StringToNum(audioModuleInfo.deviceType)); sourceInfo.volume = MAX_SINK_VOLUME_LEVEL; // 1.0f; - sourceInfo.ecType = static_cast(std::atol(audioModuleInfo.ecType.c_str())); + sourceInfo.ecType = static_cast(StringToNum(audioModuleInfo.ecType)); sourceInfo.ecAdapterName = audioModuleInfo.ecAdapter; - sourceInfo.ecSamplingRate = static_cast(std::atol(audioModuleInfo.ecSamplingRate.c_str())); + sourceInfo.ecSamplingRate = static_cast(StringToNum(audioModuleInfo.ecSamplingRate)); sourceInfo.ecFormat = TransFormatFromStringToEnum(audioModuleInfo.ecFormat); - sourceInfo.ecChannels = static_cast(std::atol(audioModuleInfo.ecChannels.c_str())); + sourceInfo.ecChannels = static_cast(StringToNum(audioModuleInfo.ecChannels)); sourceInfo.ecFrameLen = DEFAULT_MULTICHANNEL_FRAME_LEN_MS * (sourceInfo.ecSamplingRate / MS_PER_SECOND); - sourceInfo.micRef = static_cast(std::atol(audioModuleInfo.openMicRef.c_str())); - sourceInfo.micRefSamplingRate = static_cast(std::atol(audioModuleInfo.micRefRate.c_str())); + sourceInfo.micRef = static_cast(StringToNum(audioModuleInfo.openMicRef)); + sourceInfo.micRefSamplingRate = static_cast(StringToNum(audioModuleInfo.micRefRate)); sourceInfo.micRefFormat = TransFormatFromStringToEnum(audioModuleInfo.micRefFormat); - sourceInfo.micRefChannels = static_cast(std::atol(audioModuleInfo.micRefChannels.c_str())); - sourceInfo.openMicSpeaker = static_cast(std::atol(audioModuleInfo.OpenMicSpeaker.c_str())); + sourceInfo.micRefChannels = static_cast(StringToNum(audioModuleInfo.micRefChannels)); + sourceInfo.openMicSpeaker = static_cast(StringToNum(audioModuleInfo.OpenMicSpeaker)); sourceInfo.micRefFrameLen = DEFAULT_MULTICHANNEL_FRAME_LEN_MS * (sourceInfo.micRefSamplingRate / MS_PER_SECOND); return SUCCESS; } -- Gitee From 5184ae7d762a0e6d7f0399d6bd68539b78aba83e Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 27 Aug 2025 02:59:16 +0000 Subject: [PATCH 2/2] update services/audio_engine/node/src/hpae_node_common.cpp. Signed-off-by: Charles --- services/audio_engine/node/src/hpae_node_common.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/audio_engine/node/src/hpae_node_common.cpp b/services/audio_engine/node/src/hpae_node_common.cpp index 39e864637f..17b600d008 100644 --- a/services/audio_engine/node/src/hpae_node_common.cpp +++ b/services/audio_engine/node/src/hpae_node_common.cpp @@ -133,7 +133,8 @@ static long StringToNum(const std::string &str) { char *endptr; long num = strtol(str.c_str(), &endptr, BASE_TEN); - CHECK_AND_RETURN_RET_LOG(*endptr == '\0', 0, "trans str \"%{public}s\" to num failed", str.c_str()); + CHECK_AND_RETURN_RET_LOG(endptr != nullptr && *endptr == '\0', 0, + "trans str \"%{public}s\" to num failed", str.c_str()); return num; } -- Gitee