fix: replace xxd dependency with printf for hex decoding#235
fix: replace xxd dependency with printf for hex decoding#235mhduiy wants to merge 1 commit intolinuxdeepin:masterfrom
Conversation
Changed hex decoding method from xxd to printf with sed transformation to handle hex-encoded paths parameter without external dependencies. The previous implementation required xxd command which might not be available in all environments. The new approach uses sed to convert each hex pair to \x escape sequences, then printf interprets them as binary data, providing a pure bash solution that is more portable. fix: 移除对 xxd 的依赖,改用 printf 进行十六进制解码 将十六进制解码方法从 xxd 改为使用 printf 配合 sed 转换,以处理十六进制 编码的路径参数,无需外部依赖。之前的实现需要 xxd 命令,但该命令可能并非 在所有环境中都可用。新方法使用 sed 将每个十六进制对转换为 \x 转义序列, 然后使用 printf 将其解释为二进制数据,提供了一个纯 bash 解决方案,具有 更好的可移植性。 PMS: BUG-349711
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: mhduiy The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
deepin pr auto review这段代码修改的目的是将十六进制编码的字符串解码为可读文本。原代码使用 1. 语法逻辑审查
2. 代码质量审查
3. 代码性能审查
4. 代码安全审查
5. 综合改进建议如果目标是去除 方案 A:使用 Bash 内置特性(推荐,如果确定运行环境是 Bash) 如果必须替换 # 检查长度是否为偶数,否则补零或报错
if [ $(( ${#ENCODED_PARAM} % 2 )) -ne 0 ]; then
# error_exit "Invalid hex string length"
# 或者简单地补个0,视业务逻辑而定
ENCODED_PARAM="${ENCODED_PARAM}0"
fi
# 使用循环逐字节处理,最安全但最慢(适用于短字符串)
PATHS_PARAM=""
while [ -n "$ENCODED_PARAM" ]; do
# 取前两个字符
hex_byte="${ENCODED_PARAM:0:2}"
# 转换为 ASCII 并追加
PATHS_PARAM+="${hex_byte}" # 这里需要转义,见下文
# 移除已处理部分
ENCODED_PARAM="${ENCODED_PARAM:2}"
done
# 上述循环在 Bash 中可以通过 printf 更优雅地实现:
# 但要注意,直接 printf "${ENCODED_PARAM//../\\x&}" 仍然存在奇数长度问题方案 B:优化原修改(针对 Bash) 如果坚持使用 # 检查是否为有效的十六进制字符串(仅含 0-9a-fA-F)且长度为偶数
if [[ "$ENCODED_PARAM" =~ ^[0-9a-fA-F]+$ ]] && [ $(( ${#ENCODED_PARAM} % 2 )) -eq 0 ]; then
# 使用 sed 替换,并确保 printf 的错误被捕获
# 注意:这里假设是 Bash
PATHS_PARAM=$(printf '%b' "${ENCODED_PARAM//../\\x}" 2>/dev/null) || PATHS_PARAM=""
else
PATHS_PARAM=""
fi
if [ -z "$PATHS_PARAM" ]; then
error_exit "Failed to decode hex parameter: Invalid input"
fi方案 C:使用 Python(如果环境允许) 如果系统中有 Python(通常比 PATHS_PARAM=$(python3 -c "import sys, binascii; print(binascii.unhexlify(sys.stdin.read().strip()).decode('utf-8', errors='replace'))" <<< "$ENCODED_PARAM") || PATHS_PARAM=""优点: 完美处理错误、编码问题,不需要复杂的 shell 转义。 总结原 diff 的修改引入了 兼容性风险(非 POSIX shell)和 安全风险( |
Changed hex decoding method from xxd to printf with sed transformation to handle hex-encoded paths parameter without external dependencies. The previous implementation required xxd command which might not be available in all environments. The new approach uses sed to convert each hex pair to \x escape sequences, then printf interprets them as binary data, providing a pure bash solution that is more portable.
fix: 移除对 xxd 的依赖,改用 printf 进行十六进制解码
将十六进制解码方法从 xxd 改为使用 printf 配合 sed 转换,以处理十六进制
编码的路径参数,无需外部依赖。之前的实现需要 xxd 命令,但该命令可能并非
在所有环境中都可用。新方法使用 sed 将每个十六进制对转换为 \x 转义序列,
然后使用 printf 将其解释为二进制数据,提供了一个纯 bash 解决方案,具有
更好的可移植性。
PMS: BUG-349711