Skip to content

Add basic calculator functionality in test1.cpp - #4

Open
SoftSec-Tech wants to merge 1 commit into
masterfrom
SoftSec-Tech-patch-4
Open

Add basic calculator functionality in test1.cpp#4
SoftSec-Tech wants to merge 1 commit into
masterfrom
SoftSec-Tech-patch-4

Conversation

@SoftSec-Tech

Copy link
Copy Markdown
Owner

Implement a basic calculator using big_data_int class.

Implement a basic calculator using big_data_int class.

@SoftSec-Tech SoftSec-Tech left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI代码审查完成,发现 6 个问题需要关注。

Comment thread test1.cpp
#include <string>
#include "big_data_int.h"

string getString(){

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦊 AI代码审查

问题: 函数返回类型与实际返回值不匹配,应返回string对象而非指针
建议: 修改函数返回类型为string,并直接返回string对象,避免不必要的动态内存分配
严重程度: error

Comment thread test1.cpp
#include "big_data_int.h"

string getString(){
auto* str = new string();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦊 AI代码审查

问题: 手动分配了string的内存但未正确使用,存在内存泄漏风险
建议: 不应手动new string,可直接使用栈上的string对象并通过引用或直接操作进行输入
严重程度: error

Comment thread test1.cpp
auto* str = new string();
str->reserve(100);
std::cout << "请输入内容:";
std::cin.getline(str->c_str(), 100); // 直接读取到缓冲区

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦊 AI代码审查

问题: 通过c_str()修改string内容是未定义行为
建议: 使用std::getline(std::cin, *str)来安全地读取用户输入到string对象中
严重程度: error

Comment thread test1.cpp
std::cin.getline(str->c_str(), 100); // 直接读取到缓冲区

std::cout << "输入内容:" << str->c_str() << std::endl;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦊 AI代码审查

问题: 返回的是string指针而不是string对象,会导致编译错误或运行时错误
建议: 改为返回*str并删除第5行的new操作,或者重构代码以避免动态分配
严重程度: error

Comment thread test1.cpp
s_b = getString();

cout<<"Please input the operator(+,-,*,/,%):"<<endl;
string op;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦊 AI代码审查

问题: 未对用户输入的操作符进行有效性验证
建议: 在switch之前增加对op字符串长度的检查,确保只处理单字符操作符
严重程度: warning

Comment thread test1.cpp

cout<<"Please input the operator(+,-,*,/,%):"<<endl;
string op;
cin>>op;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦊 AI代码审查

问题: 直接访问op.c_str()[0]可能存在越界风险
建议: 先判断op是否为空,再访问其第一个字符
严重程度: warning

Comment thread test1.cpp
string getString(){
auto* str = new string();
str->reserve(100);
std::cout << "请输入内容:";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mttests

Comment thread test1.cpp
break;
default:
cout<<"Wrong operator input"<<endl;
return -1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这应该是xxx提的吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants