Skip to content

Commit a4c57d8

Browse files
authored
[0040] 修复 s7.c 中 memmove 编译警告 (#790)
1 parent 39500e4 commit a4c57d8

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

devel/0040.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [0040] 修复 s7.c 中 memmove 编译警告
2+
3+
## 任务相关的代码文件
4+
- `src/s7.c`
5+
6+
## 如何测试
7+
```bash
8+
# 1. 清理并重新构建
9+
xmake f -c && xmake b goldfish
10+
11+
# 2. 确认警告消失(不应再出现 -Wstringop-overflow 警告)
12+
```
13+
14+
## 2026-05-17 修复 memmove 编译警告
15+
16+
### What
17+
修复 `src/s7.c:14298` 处 GCC 的 `-Wstringop-overflow` 编译警告:
18+
19+
```
20+
src/s7.c:14298:3: warning: 'memmove' specified bound between 9223372036854775808 and
21+
18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
22+
14298 | memmove((void *)(sc->num_to_str + spaces), (const void *)src, len);
23+
| ^~
24+
```
25+
26+
`insert_spaces` 函数开头添加边界检查,消除警告。
27+
28+
### Why
29+
虽然所有调用者都已保证 `width > len`,但 GCC 静态分析器无法跨函数完全推导出 `len` 始终为非负数。当编译器考虑 `len` 为负值的可能性时,发现其转换为 `size_t` 后会变成巨大的无符号值,从而触发 `-Wstringop-overflow` 警告。这个无害的警告可能掩盖其他真正的溢出问题,并且在启用 `-Werror` 时会导致编译失败。
30+
31+
### How
32+
`insert_spaces` 函数内添加显式的提前返回:
33+
34+
```c
35+
if ((spaces <= 0) || (len <= 0)) return;
36+
```
37+
38+
这样编译器可以静态推导出:进入后续代码时,`spaces > 0`(指针运算安全)且 `len > 0`(传给 `memmove` 的 size 参数为正),从而消除警告。

src/s7.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14289,6 +14289,7 @@ static char *floatify(char *str, s7_int *nlen)
1428914289
static void insert_spaces(s7_scheme *sc, const char *src, s7_int width, s7_int len)
1429014290
{
1429114291
s7_int spaces = width - len;
14292+
if ((spaces <= 0) || (len <= 0)) return;
1429214293
if (width >= sc->num_to_str_size)
1429314294
{
1429414295
sc->num_to_str_size = width + 1;

0 commit comments

Comments
 (0)