c - 这个子字符串在 C 中安全吗?
问题描述
我需要从不同长度的字符串中提取子字符串。我想避免使用malloc。
这安全吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
const char *PATTERN1 = "<Abcd.";
const char *PATTERN2 = ">";
const char *PATTERN3 = "<Fghi.";
char *begin, *end;
int len;
char result[50];
// lenght of toFind could be different i.e. "<Abcd.toGet>" or "<Abcd.toGettoGettoGet>" always less than 50
char *toFind[50] = {"<Abcd.toGettoGet>","<Abcd.toGettoGetoGet>","<Abcd.toGet>"};
int element = 3;
int i = 0;
for (i = 0; i < element ; i++) {
begin = strstr(toFind[i], PATTERN1);
printf("Begin: %s \n", begin);
if (begin == NULL) {
perror("Null begin\n");
}
begin += strlen(PATTERN1);
printf("Begin2: %s \n", begin);
end = strstr(begin, PATTERN2);
printf("End: %s \n", end);
if (end == NULL) {
perror("Null end\n");
}
len = end - begin;
printf("Len: %d \n", len);
strncpy(result, begin, len);
result[len] = '\0';
printf("Result = %s\n",result);
sleep(5);
}
return 0;
}
我想知道这是否是一种正确且安全的方法,以避免分段错误或内存损坏/泄漏。尤其是 *begin 和 *end 它是如何初始化的。
解决方案
您需要解决一些问题。
- 不要只在
begin
orend
为 NULL 时继续。
else
您应该在放置以下代码的位置添加一个部分。例子:
if (begin == NULL) {
perror("Null begin\n");
} else {
begin += strlen(PATTERN1);
...
...
if (end == NULL) {
perror("Null end\n");
} else {
len = end - begin;
...
...
}
}
- 考虑写作
result
越界。
该result
数组是固定大小的,但len
可以得到任何大小。所以使用strncpy(result, begin, len);
不会保护你免受越界写入。使用前添加检查strncpy
- 如果
strstr
返回 NULL,则不打印字符串。
IE
begin = strstr(toFind[i], PATTERN1);
printf("Begin: %s \n", begin); // Check for NULL **before** printing
推荐阅读
- jira - 如何从 Jira Webhook 响应中获取日期时间
- list - 获取自定义管理模块中所有类别和子类别的列表
- reactjs - 如何在使用 react-hook 调用 setState 后使用更新的状态
- prolog - 为什么我的谓词不起作用,而类似的谓词却起作用?
- git - github.com:权限被拒绝(公钥)
- php - 如何在 PHP 中将数组键添加到子数组
- android - 无法将 RxJava 与 Hengam SDK 一起使用
- html - 如何在 CSS 中覆盖引导导航栏的折叠
- r - 在 R 中,如何选择在某个行索引中包含某个值的某些列?
- android - 如何在 Android 应用程序中检查它正在使用加密共享首选项并以编程方式安全地迁移到它