libevent: řetězec/buffer musí být pro použití jako string ukončen \0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 21 Nov 2016 22:48:29 +0100
changeset 43b54d76467040
parent 42 5bd10c0ae650
child 44 896eca3d40d8
libevent: řetězec/buffer musí být pro použití jako string ukončen \0
c++/domain-socket-bridge/domain-socket-bridge.cpp
     1.1 --- a/c++/domain-socket-bridge/domain-socket-bridge.cpp	Sun Nov 20 21:00:47 2016 +0100
     1.2 +++ b/c++/domain-socket-bridge/domain-socket-bridge.cpp	Mon Nov 21 22:48:29 2016 +0100
     1.3 @@ -148,10 +148,11 @@
     1.4  	struct event_base *base = bufferevent_get_base(bev);
     1.5  
     1.6  	size_t len = evbuffer_get_length(input);
     1.7 -	char *data = (char*) malloc(len);
     1.8 +	char *data = (char*) malloc(len + 1);
     1.9 +	data[len] = 0; // as a string must be terminated by \0 
    1.10  	evbuffer_copyout(input, data, len);
    1.11  
    1.12 -
    1.13 +	//TODO: there might already be \0 in the byte stream, only the part before the first \0 will be formatted as a string
    1.14  	string dataFormated(data);
    1.15  	dataFormated = regex_replace(dataFormated, regex("\\n"), "\\n");
    1.16  	printf("%4d %8s '%s'\n", connectionId, "IN", dataFormated.c_str());