2020-05-25

DAILY BOOK list

  1. The future is so confused that I have to take a strong courage to break through.

  2. The power developed by a believer is greater than that developed by an interested one.在这里插入图片描述

  3. All the brags will turn into loud slaps, either on their own faces or on those who despise them.

  4. Optimistic people can rise again and again. Pessimistic people often fail because of their lack of self-confidence.

编译时报错: In file included from /usr/include/string.h:535, from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blob.h:25, from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.h:20, from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c:16: In function ‘strcpy’, inlined from ‘blobmsg_new’ at /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c:196:2: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:79:10: error: ‘__builtin_strcpy’ offset 6 from the object at ‘attr’ is out of the bounds of referenced subobject ‘name’ with type ‘uint8_t[]’ {aka ‘unsigned char[]’} at offset 6 [-Werror=array-bounds] 79 | return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c:16: /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c: In function ‘blobmsg_new’: /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.h:42:17: note: subobject ‘name’ declared here 42 | uint8_t name[];
10-11
这个编译错误提示是由于在调用 `strcpy` 函数时,尝试将字符串复制到一个大小不足的数组中,导致访问越界。具体来说,`blobmsg_new` 函数中使用 `strcpy` 将字符串复制到 `attr->name` 数组中,但 `name` 数组的声明是 `uint8_t name[];`(柔性数组成员),而实际分配的内存可能不足以容纳要复制的字符串内容。 ### 错误分析: - 错误信息中的 `offset 6 from the object at 'attr'` 表示在 `attr` 结构体的偏移量 6 处(即 `name` 字段)出现了越界。 - `strcpy` 是不安全的函数,因为它不会检查目标缓冲区的大小,可能导致缓冲区溢出。 - 柔性数组成员 `name[]` 需要在结构体分配时额外分配足够的内存空间,否则使用 `strcpy` 时会越界。 ### 解决方法: 1. **确保为柔性数组成员分配足够的内存**: - 在定义包含柔性数组成员的结构体时,必须手动分配足够的内存,例如: ```c struct my_struct { int some_field; uint8_t name[]; // 柔性数组 }; // 分配足够的内存,例如 100 字节用于 name struct my_struct *s = malloc(sizeof(struct my_struct) + 100); ``` - 确保 `strcpy` 复制的字符串长度不超过分配的内存大小。 2. **使用更安全的字符串复制函数**: - 替换 `strcpy` 为 `strncpy`,并指定最大复制长度,避免越界: ```c strncpy((char *)attr->name, name, len); attr->name[len] = '\0'; // 确保字符串以 '\0' 结尾 ``` 3. **检查传递给 `blobmsg_new` 的参数**: - 确保传入的字符串长度不超过 `name` 数组的分配大小。 - 如果字符串长度不确定,可以先动态分配内存再进行复制。 4. **调整结构体设计**: - 如果无法确定字符串长度,可以考虑使用固定大小的数组或动态分配内存管理。 ### 示例修复代码: ```c struct blob_attr *blobmsg_new(const char *name) { size_t name_len = strlen(name) + 1; // 包含结尾的 '\0' struct blob_attr *attr = malloc(sizeof(*attr) + name_len); if (!attr) return NULL; // 使用安全的复制方式 memcpy(attr->name, name, name_len); attr->name[name_len - 1] = '\0'; // 确保字符串以 '\0' 结尾 return attr; } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值