首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于char阵列C++的Flawfinder (CWE-119!/CWE-120)

用于char阵列C++的Flawfinder (CWE-119!/CWE-120)
EN

Stack Overflow用户
提问于 2020-07-18 14:35:38
回答 1查看 711关注 0票数 0

我有一个char数组,定义如下

代码语言:javascript
复制
char buffer[100];

当我对点击率进行Flawfinder扫描时,我得到的是:

代码语言:javascript
复制
(buffer) char:
  Statically-sized arrays can be improperly restricted, leading to potential
  overflows or other issues (CWE-119!/CWE-120). Perform bounds checking, use
  functions that limit length, or ensure that the size is larger than the
  maximum possible length.

我知道我必须在需要时进行检查,以确保我的代码是免费的,但是我们有任何方法来解决这个问题(以其他方式定义char数组)并使Flawfindr输出不受任何影响吗?

更新

下面是函数的完整代码,以防有帮助

代码语言:javascript
复制
std::string MyClass::randomGenerator(odb::nullable<int> maxLength) {
    
    struct timeval tmnow;
    
    struct tm *tm;
    
    char buf[100];
    
    gettimeofday(&tmnow, NULL);
    
    tm = localtime(&tmnow.tv_sec);
    
    strftime(buf, 100, "%m%d%H%M%S", tm);
    
    string micro = std::to_string(((int)tmnow.tv_usec / 10000));
    
    strlcat(buf, micro.c_str(), sizeof(buf));
    
    std::stringstream stream;
    
    stream << std::hex << stoll(buf);
    
    std::string result(stream.str());
    
    Utilities::find_and_replace(result, "0", "h");
    
    Utilities::find_and_replace(result, "1", "k");
    
    std::transform(result.begin(), result.end(),result.begin(), ::toupper);
    
    if (maxLength) {
        
        return result.substr(result.size() - maxLength.get(), result.size() - 1);
        
    } else {
        
        return result ;
        
    }
    
}
EN

回答 1

Stack Overflow用户

发布于 2020-07-19 03:51:46

Flawfinder确实是一个稍微美化了的grep -它不是一个真正的静态分析工具来进行数据流分析,所以我总是用健康的盐来获取它的输出!

真正应该编写这段代码的方法是使用C运行时函数编写真正的C++代码,而不是美化-C,这些函数绝对会导致内存损坏问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62969768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档