0

I'm using libevent to do simple POST http server, but i find strange issue - evbuffer_copyout write to *data sended data with overhead (changed every time). As u see in code, i was do fix by cut unnecessary symbols, but it not looks like correct solution. Here that i receive: enter image description here

bellow is code of callback:

size_t len = evbuffer_get_length(evhttp_request_get_input_buffer(r));
struct evbuffer *in_evb = evhttp_request_get_input_buffer(r);

size_t lenBuffer = evbuffer_get_length(in_evb);

char *data = malloc(len);
evbuffer_remove(in_evb, data, len);
NSString *final = [NSString stringWithCString:data encoding:NSASCIIStringEncoding];
NSString *finalFinal = [final substringWithRange:NSMakeRange(0, len)];

NSLog(@"==>lenBuffer:%zu len:%zu data:%@",lenBuffer,len,finalFinal);
evhttp_add_header(evhttp_request_get_output_headers(r),
                  "Content-Type", "text/html");
evhttp_send_reply(r, 200, "OK", in_evb);
free(data);
user170317
  • 1,152
  • 2
  • 11
  • 22

1 Answers1

1

When you allocate memory through malloc, it filled with some garbage. You fill this memory with useful data, but probably it doesn't contain string terminator character '\0' So, debugger shows you data which were obtained from buffer following with some garbage.

CyberDem0n
  • 14,545
  • 1
  • 34
  • 24
  • it's wrong. bcs if i will do: NSLog(@"==>lenBuffer:%zu len:%zu data:/%s/",lenBuffer,len,data); i receive: ==>lenBuffer:15 len:15 data:/{"test":"test"}0®µ±y€/ - this is approve that it not debugger issue. – user170317 Aug 15 '12 at 16:39
  • {"test":"test"} -- 15 bytes of data. You want to get string. But strings in `c` must be null terminated. So just malloc len+1 and set data[len] = '\0'; – CyberDem0n Aug 15 '12 at 16:52