Thread: Dynamic memory allocation and socket programming - a double pointer

  1. #1
    Registered User
    Join Date
    Mar 2020

    Dynamic memory allocation and socket programming - a double pointer

    Hello, I am a beginner in C and am trying to learn very basic sockets and socket programming. Since I am C beginner, dynamic memory, memory management and pointers are all very new to me-and in particular cannot resolve a bad bug in my code, I have been working on it for a while, and thought I would ask here. I would appreciate any help or thoughts about the following code/errors, as it may be very obvious to you.

    Code below-which sets up a socket, client and is attempting to read in buffered input from client and print to stdout. I believe I may have a fundamental misunderstanding about memory or pointers:

    buggy function:

    //search buffer for \r\n, copy complete message into newly-allocated NULL terminated string. Remove message from buffer by moving remaining content to the front of buffer. inbuffer refers to valid searchable characters in buffer (not junk).
    //find_nn loops through first inbuf elements and searches for \r\n. If found returns index of first \r+2, otherwise -1 means no \r\n is found.
    int get_msg(char** s, char* buffer, int* inbuffer){
         int n = find_nn(buffer, *inbuf); 
               return -1;
          char* new_s = malloc(sizeof(char)*30); //max size of buffer
              return -1;
          memcpy(new_s, buffer, n);
          memmove(buffer, buffer+n; inbuffer);
          return 0;

    relevant driver code:

    //reads from a connected client until receives a full message containing a \r\n.
    //client is a standard client struct 
        char *m;
        while(!get_msg(&m, clients->buffer, &(clients->inbuf))){
            printf("Message: %s\n", m);
    //exit if client closes
    It compiles, but the following errors when I try to run the server/client (client writes broken messages to server, which I am aiming to reassemble using \r\n).


    ==2193==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60400000003c at pc 0x7fd9b1c04deb bp 0x7ffe1b2e9620 sp 0x7ffe1b2e8dc8
    READ of size 36 at 0x60400000003c thread T0
    #0 0x7fd9b1c04dea in __interceptor_memmove (/usr/lib/x86_64-linux-gnu/
    #1 0x560117f75e62 in get_msg:142

    Further, it seems that the first iteration of my loop works as expected, but i seem to overflow when trying to shift or change my buffer. I appreciate any suggestions you may have.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Please post actual code that you've ran then copy/pasted, not something you just typed up.

    What is inbuf? What is newline? (Presumably inbuffer and n.)
    And you have a semicolon in one of your function parameter lists.
    So this is clearly not code you've ran or even compiled.

    Why are you only allocating 30 bytes for new_s? Surely you would allocate n bytes.
    Otherwise with a cursory glance it looks okay.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-21-2015, 11:34 PM
  2. Double Pointers and dynamic memory allocation
    By Casanova411 in forum C Programming
    Replies: 6
    Last Post: 11-16-2013, 05:14 PM
  3. Double Pointer, dynamic allocation help
    By Trey Brumley in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2013, 07:35 AM
  4. dynamic memory allocation using pointer to pointers
    By mp252 in forum C++ Programming
    Replies: 12
    Last Post: 06-22-2013, 05:34 PM
  5. Double Pointer Memory Allocation: Problems
    By loopshot in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 05:38 PM

Tags for this Thread