Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;运行时检查失败#2-围绕变量';文件路径';“腐败了”;在调试模式下_C++_Visual Studio_Stack Overflow - Fatal编程技术网

C++ &引用;运行时检查失败#2-围绕变量';文件路径';“腐败了”;在调试模式下

C++ &引用;运行时检查失败#2-围绕变量';文件路径';“腐败了”;在调试模式下,c++,visual-studio,stack-overflow,C++,Visual Studio,Stack Overflow,我在使用winsock2时遇到了此函数的问题。当程序以调试模式离开此函数时,我收到2条语句(仅在退出此函数时): “运行时检查失败#2-变量'filePath'周围的堆栈已损坏。” “运行时检查失败#2-变量'recBuf'周围的堆栈已损坏。” 我在VS2008 C++(控制台应用程序)编程。 在发布模式下,此语句不存在 所以我在等待回复,非常感谢您的帮助;) 功能: #include "stdafx.h" #include "server.h" bool Server::fileReceiv

我在使用winsock2时遇到了此函数的问题。当程序以调试模式离开此函数时,我收到2条语句(仅在退出此函数时):

“运行时检查失败#2-变量'filePath'周围的堆栈已损坏。”

“运行时检查失败#2-变量'recBuf'周围的堆栈已损坏。”

我在VS2008 C++(控制台应用程序)编程。 在发布模式下,此语句不存在

所以我在等待回复,非常感谢您的帮助;)

功能:

#include "stdafx.h"
#include "server.h"

bool Server::fileReceive()
{
    char recBuf[MAX_PACKET_BUFOR] ={0};
    char filePath[100] = {0};
    int size = 0;
    int var = 0;
    char key = 0;
    int tmp=0;

    FILE *fPtr = NULL;

    if(recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0) == 0)
    {
        cerr << "Receive ERROR" << endl;
        system("pause");
        return false;
    }
    if(strcmp(recBuf, START_HEADER) != 0)
        return false;

    if(send(clientSocket, ACK, 3, 0 ) == SOCKET_ERROR)
    {
        cerr << "Send ERROR" << endl;
        system("pause");
        return false;
    }
    if(recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0) == 0)
    {
        cerr << "Receive ERROR" << endl;
        system("pause");
        return false;
    }
    if(send(clientSocket, ACK, 3, 0 ) == SOCKET_ERROR)
    {
        cerr << "Send ERROR" << endl;
        system("pause");
        return false;
    }
    strcpy_s(filePath, sizeof(recBuf), recBuf);
    cout << "Przyslano plik o nazwie: \"" << filePath << "\"";
    memset(recBuf, 0, sizeof(recBuf));

    if(recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0) == 0)
    {
        cerr << "Receive ERROR" << endl;
        system("pause");
        return false;
    }
    if(send(clientSocket, ACK, 3, 0 ) == SOCKET_ERROR)
    {
        cerr << "Send ERROR" << endl;
        system("pause");
        return false;
    }
    size = atoi(recBuf);
    cout << " i rozmiarze: "<< size << "B,\nCo chcesz zrobic?" << endl;
    cout << "1 - odbierz i zmien nazwe\n2- odbierz i zapisz do katalogu programu\n3 - odrzuc" << endl;
    key = _getch();

    if(key == '1' || key == '2')
    {
        if(key == '1')
        {
            cout << "Podaj sciezke dla nowego pliku w formie: Dysk:\\sciezka\\plik.rozszerzenie" << endl;
            cout << "(wpisz sama nazwe, a plik zostanie umieszczony w katalogu programu)" << endl;
            cin >> filePath;
        }

        if(fopen_s(&fPtr, filePath, "wb") != 0)
            return false;

        cout << "Odbieranie w toku...\nProsze czekac" << endl;

        while(size > 0)
        {
            memset(recBuf, 0, MAX_PACKET_BUFOR);
            tmp = 0;
            if(size <= 200000)
                cout << "break" << endl;
            if(size>=MAX_PACKET_SIZE)
            {
                do
                {
                    var = recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0);
                    if(var == 0)
                    {
                        cerr << "Receive ERROR" << endl;
                        system("pause");
                        size = 0;
                    }
                    else
                    {
                        tmp += var;
                        fwrite(recBuf, var, 1, fPtr);
                    }
                }while(tmp < MAX_PACKET_SIZE && size != 0);
                                    if(send(clientSocket, ACK, 3, 0 ) == SOCKET_ERROR)
                    {
                        cerr << "Send ERROR" << endl;
                        system("pause");
                        return false;
                    }
            }
            else
            {
                while(tmp != size && size != 0)
                {
                    var = recv(clientSocket, recBuf, size, 0);
                    if(var == 0)
                    {
                        cerr << "Receive ERROR" << endl;
                        system("pause");
                        size = 0;
                    }
                    else
                    {
                        tmp += var;
                        fwrite(recBuf, var, 1, fPtr);
                    }
                }
                if(send(clientSocket, ACK, 3, 0 ) == SOCKET_ERROR)
                {
                    cerr << "Send ERROR" << endl;
                    system("pause");
                    return false;
                }
            }
            size -= tmp;
            size = (size < 0) ? 0 : size;
        }
        memset(filePath, 0, sizeof(filePath));
        memset(recBuf, 0, MAX_PACKET_BUFOR);
        fclose(fPtr);
    }
    return true;
}
#包括“stdafx.h”
#包括“server.h”
bool服务器::fileReceive()
{
char recBuf[MAX_PACKET_BUFOR]={0};
char filePath[100]={0};
int size=0;
int-var=0;
字符键=0;
int-tmp=0;
文件*fPtr=NULL;
if(recv(clientSocket,recBuf,最大数据包大小,0)=0)
{

cerr错误在下面一行

strcpy_s(filePath, sizeof(recBuf), recBuf); 
这里您说明的
filePath
的大小是
sizeof(recBuf)
,情况并非如此。
filePath
的大小是100,而
sizeof(recBuf)
的大小是1000010。结果是
filePath
上的缓冲区溢出以及您看到的错误消息


此外,代码似乎假设
recv
调用将正确地向
recBuf
缓冲区添加空终止符。情况并非如此,如果未向缓冲区添加0,则
strcpy_s
函数也可能会失败。

thx非常感谢您的快速回复。它现在可以正常工作:)
strcpy_s(filePath, sizeof(recBuf), recBuf);