C:free()无效下一个尺寸(快)

时间:2017-05-26 17:30:13

标签: c sockets

我看到同样问题的主题很少,但我仍然无法解决我的问题。所以这是我的代码 - 使用XOR加密的C语言套接字编程

当服务器和客户端连接时: - 用户发送消息,例如:hello - 服务器响应,例如:(服务器):你好(用户说什么)

我在服务器的代码中遇到了这个错误,函数handleClient(..)

谢谢 抱歉我的英文不好:|

// server.c

/**
* Load libraries
**/
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "xor.h"

/**
* Define functions
**/
void handleError(char errorMessage[]);
void handleClient(int clientSocket);
void closeSocket(int serverSocket);

/**
* Define global variables
**/
int serverSocket;

/**
* Define constants
**/
#define DEFAULT_PORT 1213
#define QUEUE_LIMIT 10
#define BUFFER_LEN 64

/**
* Main function
**/
int main(int argc, char *argv[])
{
    printf("------------SERVER------------\n");

    int clientSocket;
    struct sockaddr_in serverAddr;
    struct sockaddr_in clientAddr;
    unsigned short serverPort;
    unsigned int clientLen;

    if(argc != 2) {
        printf("Wrong argument!\n");
        exit(1);    
    }

    serverPort = atoi(argv[1]);

    if((serverSocket = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
        handleError("socket() fail\n");
    }

    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_UNIX;
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddr.sin_port = htons(serverPort);

    if(bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) {
        handleError("bind() fail\n");
    }

    if(listen(serverSocket, QUEUE_LIMIT) < 0) {
        handleError("listen() fail\n"); 
    }

    clientLen = sizeof(clientAddr);
    if((clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &clientLen)) < 0) {
        handleError("accept() fail\n");     
    } else {
        handleClient(clientSocket);
    }

    return 0;
}

/**
* Display error message
* @param char message
**/
void handleError(char errorMessage[]) 
{
    printf("ERROR: %s", errorMessage);
    closeSocket(serverSocket);
    exit(1);
}

/**
* Handle client connection
* @param int client socket
**/
void handleClient(int clientSocket)
{
    char *buffer = malloc(BUFFER_LEN+1);
    char *key = "123456";
    char pre[BUFFER_LEN];

    while(clientSocket > 0) {
        if(recv(clientSocket, buffer, BUFFER_LEN, 0) > 0) {
            buffer = xor_decrypt(key, buffer);
            printf(">> User %d: %s", clientSocket, buffer);

            strcpy(pre, "(Server): ");
            strcat(pre, buffer);
            strcpy(buffer, pre);
            if(send(clientSocket, xor_encrypt(key, buffer), BUFFER_LEN, 0) < 0) {
                printf("send() fail\n");
                break;      
            }   
            strcpy(buffer, "");
            free(buffer);
            buffer = malloc(BUFFER_LEN+1);
        }
    }

    free(buffer);
    closeSocket(clientSocket);
}

/**
* Close socket
* @param socketid sockid that opened
*/
void closeSocket(int socket)
{
    int status = shutdown(socket, SHUT_RDWR);
    switch(status) {
        case 0:
            printf("Socket close successful!\n");
            break;
        case -1:
            printf("Can't close socket!\n");
            break;
        default:
            printf("shutdown() fail\n");
            break;
    }
}

// xor.c

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "xor.h"

char *xor_encrypt(const char *key, char *string) 
{   
    int lenStr = strlen(string);
    int lenKey = strlen(key);

    char *encrypted = malloc(lenStr + 1);

    int i;
    for(i = 0; i < lenStr; i++) {
        encrypted[i] = string[i]^key[i % lenKey];
    }

    return encrypted;
}

char *xor_decrypt(const char *key, char *string) {
    return xor_encrypt(key, string);
}   

Running

2 个答案:

答案 0 :(得分:0)

在这段代码中:

char *xor_encrypt(const char *key, char *string) 
{   
    int lenStr = strlen(string);
    int lenKey = strlen(key);

    char *encrypted = malloc(lenStr + 1);

    int i;
    for(i = 0; i < lenStr; i++) {
        encrypted[i] = string[i]^key[i % lenKey];
    }
    /// at this point, new buffer is not guaranteed to be NULL terminated

    return encrypted;
}

您似乎没有将空终止符附加到加密缓冲区的末尾,并且由于XOR操作,无法保证或终止NULL终结符。

添加以下行:

...
for(i = 0; i < lenStr; i++) {
    encrypted[i] = string[i]^key[i % lenKey];
}
encrypted[i] = 0;//add this line to explicitly NULL terminate new buffer
...

答案 1 :(得分:0)

我用这个解决了我的问题

P / s:有人用这段代码回答了我的问题,但他删除了他的答案! @@ 谢谢你的回答!!!

int lenStr = strlen(string);
int lenKey = strlen(key);

int lenStr = strlen(string) + 1;
int lenKey = strlen(key) + 1;