[Network]网络连接

#include <iostream>
#include <algorithm>
#include <map>
#include <cstring>
#include <cstdio>
using namespace std;
int n;
map <string,int> server;


bool check_for(string ss){
	int len = ss.size();
	string gs = "";
	for(int i = 0;i < len;i++) {
		if(ss[i] == '.' || ss[i] == ':'){
			gs += ss[i];
		}
	}
	if(gs == "...:"){
		return 1;
	}
	else{
		return 0;
	}
}
long long int a,b,c,d,e;


int main(){
	string op,ad;
	//ios::sync_with_stdio(false);
	//cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 0;i < n;i ++){
		a = 0;b = 0;c = 0;d = 0;e = 0;
		cin >> op >> ad;
		if(check_for(ad)){
			sscanf(ad.c_str(),"%lld.%lld.%lld.%lld:%lld",&a,&b,&c,&d,&e);
			if(a < 256 && b < 256 && c < 256 && d < 256 && e < 65536 && op == "Server"){
				if(server.count(ad) < 1 || server.empty()){
					server.insert(pair<string,int>(ad,i+1));
					cout << "OK\n";
					sidx++;
				}else{
					cout << "FAIL\n";
					continue;
				}
			}else if(a < 256 && b < 256 && c < 256 && d < 256 && e < 65536 && op == "Client"){
				if(server.find(ad) != server.end()){
					cout << server[ad] << "\n";
				}else{
					cout << "FAIL\n";
				}
			}else{
				cout << "ERR\n";
				continue;
			}
		}else{
			cout << "ERR\n";
			continue;
		}
	}
	
	return 0;
}

1 comments

  • @ 2025-10-19 23:37:17

    豆包nb 其实没那么nb

    下面是在你的源代码中用注释标注的错误,每个错误都附带了详细说明:

    
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    int n;
    map <string,int> server;
    
    
    bool check_for(string ss){
        int len = ss.size();
        string gs = "";
        for(int i = 0;i < len;i++) {
            if(ss[i] == '.' || ss[i] == ':'){
                gs += ss[i];
            }
        }
        // 错误1:格式检查不严谨
        // 这里仅检查了分隔符顺序是"...:",但未检查:
        // 1. 是否有且仅有一个冒号
        // 2. 点和冒号的总数量是否正确
        // 3. 分隔符是否在正确的位置(如冒号必须在最后)
        // 例如"192.168.1:1.80"会被误判为合法
        if(gs == "...:"){
            return 1;
        }
        else{
            return 0;
        }
    }
    long long int a,b,c,d,e;
    int sidx = 1;  // 错误2:此变量无实际用途,服务器编号应使用当前循环的i+1
    
    
    int main(){
        string op,ad;
        //ios::sync_with_stdio(false);
        //cin.tie(0), cout.tie(0);
        cin >> n;
        for(int i = 0;i < n;i ++){
            a = 0;b = 0;c = 0;d = 0;e = 0;
            cin >> op >> ad;
            if(check_for(ad)){
                // 错误3:使用sscanf读取可能导致错误
                // 1. 无法检查前导零(如"01.0.0.0:80"会被接受)
                // 2. 无法检查数字是否仅由纯数字组成(如"192.a68.1.1:80"可能被部分读取)
                // 3. 无法处理超范围的数字(如"256.0.0.0:80"会被错误读取)
                sscanf(ad.c_str(),"%lld.%lld.%lld.%lld:%lld",&a,&b,&c,&d,&e);
                
                // 错误4:范围判断错误
                // 条件应该是a <= 255而不是a < 256(虽然结果相同,但逻辑不清晰)
                // 更严重的是e的范围判断,e应该<=65535而不是<65536(同上,但逻辑错误)
                // 同时缺少对a,b,c,d,e非负性的检查(虽然输入是数字,但可能为空或负数)
                if(a < 256 && b < 256 && c < 256 && d < 256 && e < 65536 && op == "Server"){
                    // 错误5:map判断逻辑冗余
                    // server.empty()判断多余,因为count(ad)<1已经包含了空map的情况
                    if(server.count(ad) < 1 || server.empty()){
                        server.insert(pair<string,int>(ad,i+1));
                        cout << "OK\n";
                        sidx++;  // 错误6:sidx自增无意义,从未被使用
                    }else{
                        cout << "FAIL\n";
                        continue;  // 错误7:此处continue多余,因为是if-else结构
                    }
                }else if(a < 256 && b < 256 && c < 256 && d < 256 && e < 65536 && op == "Client"){
                    if(server.find(ad) != server.end()){
                        cout << server[ad] << "\n";
                    }else{
                        cout << "FAIL\n";
                    }
                }else{
                    cout << "ERR\n";
                    continue;  // 错误8:此处continue多余
                }
            }else{
                cout << "ERR\n";
                continue;  // 错误9:此处continue多余
            }
        }
        
        return 0;
    }
    

    总结主要错误类型: 格式验证漏洞:仅通过分隔符顺序判断格式,未检查各部分是否为纯数字、是否有前导零等关键问题 数值范围检查不严谨:虽然结果可能正确,但判断逻辑不规范,且缺少非负性检查 输入处理缺陷:使用 sscanf 无法处理前导零和非法字符,导致错误的地址被判定为合法 逻辑冗余:存在多余的 continue 和判断条件,影响代码可读性和效率 无效变量:sidx 变量无实际用途,属于代码冗余 这些错误会导致代码在处理包含前导零、特殊格式或边界值的测试用例时出现错误判断。例如,"01.0.0.0:80" 这样的地址会被错误地判定为合法,而实际上它包含多余的前导零,应该被视为无效地址。

    🤣 4
    • 1