Add basic calculator functionality in test1.cpp - #4
Conversation
Implement a basic calculator using big_data_int class.
SoftSec-Tech
left a comment
There was a problem hiding this comment.
AI代码审查完成,发现 6 个问题需要关注。
| #include <string> | ||
| #include "big_data_int.h" | ||
|
|
||
| string getString(){ |
There was a problem hiding this comment.
🦊 AI代码审查
问题: 函数返回类型与实际返回值不匹配,应返回string对象而非指针
建议: 修改函数返回类型为string,并直接返回string对象,避免不必要的动态内存分配
严重程度: error
| #include "big_data_int.h" | ||
|
|
||
| string getString(){ | ||
| auto* str = new string(); |
There was a problem hiding this comment.
🦊 AI代码审查
问题: 手动分配了string的内存但未正确使用,存在内存泄漏风险
建议: 不应手动new string,可直接使用栈上的string对象并通过引用或直接操作进行输入
严重程度: error
| auto* str = new string(); | ||
| str->reserve(100); | ||
| std::cout << "请输入内容:"; | ||
| std::cin.getline(str->c_str(), 100); // 直接读取到缓冲区 |
There was a problem hiding this comment.
🦊 AI代码审查
问题: 通过c_str()修改string内容是未定义行为
建议: 使用std::getline(std::cin, *str)来安全地读取用户输入到string对象中
严重程度: error
| std::cin.getline(str->c_str(), 100); // 直接读取到缓冲区 | ||
|
|
||
| std::cout << "输入内容:" << str->c_str() << std::endl; | ||
|
|
There was a problem hiding this comment.
🦊 AI代码审查
问题: 返回的是string指针而不是string对象,会导致编译错误或运行时错误
建议: 改为返回*str并删除第5行的new操作,或者重构代码以避免动态分配
严重程度: error
| s_b = getString(); | ||
|
|
||
| cout<<"Please input the operator(+,-,*,/,%):"<<endl; | ||
| string op; |
There was a problem hiding this comment.
🦊 AI代码审查
问题: 未对用户输入的操作符进行有效性验证
建议: 在switch之前增加对op字符串长度的检查,确保只处理单字符操作符
严重程度: warning
|
|
||
| cout<<"Please input the operator(+,-,*,/,%):"<<endl; | ||
| string op; | ||
| cin>>op; |
There was a problem hiding this comment.
🦊 AI代码审查
问题: 直接访问op.c_str()[0]可能存在越界风险
建议: 先判断op是否为空,再访问其第一个字符
严重程度: warning
Implement a basic calculator using big_data_int class.