2010-10-21 3 views
3

J'ai un problème avec cette fonction en utilisant winsock2. Lorsque le programme quitte cette fonction en mode débogage je reçois 2 déclarations (seulement en sortant de cette fonction):"Échec de vérification au moment de l'exécution # 2 - Empilement autour de la variable 'filePath' a été corrompu" en mode débogage

«Run-Time Échec de la vérification # 2 - Stack autour de la variable 'filePath' a été corrompu »

« Run -Time Check Failure # 2 - La pile autour de la variable 'recBuf' a été corrompue. "

Je suis en train de programmer en VS2008 C++ (application console). En mode Release, ces instructions n'existent pas.

Je suis en attente de réponse et merci beaucoup pour votre aide;)

Fonction:

#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

#pragma once 

#include "targetver.h" 

#include <stdio.h> 
#include <tchar.h> 
#include <winsock2.h> 
#include <iostream> 
#include <cstdlib> 
#include <conio.h> 

using namespace std; 

#define START_HEADER "FileSend" 
#define MAX_PACKET_SIZE 100000 
#define MAX_PACKET_BUFOR 100010 
#define ACK "ACK" 

Répondre

4

Le bogue est dans la ligne suivante

strcpy_s(filePath, sizeof(recBuf), recBuf); 

Ici vous indiquez que la taille de filePath est sizeof(recBuf) ce qui n'est pas le cas. La taille de filePath est 100 tandis que sizeof(recBuf) est 1000010. Le résultat est un débordement de tampon sur filePath et le message d'erreur que vous voyez.

En outre, le code semble supposer que l'appel recv ajoutera correctement un terminateur null au tampon recBuf. Ce n'est pas le cas et si un 0 n'est pas ajouté dans le tampon, la fonction strcpy_s échouera potentiellement.

+0

Merci beaucoup pour votre réponse rapide. Cela fonctionne maintenant sans échecs :) – Meler

Questions connexes