I'm trying to read a message that contains 2 strings. This message contains 2 strings that could be anything, and it is sent over a socket. 

Note that I'm using C in Ubuntu environment. 

The format of the message is, in a single `void* buffer`: 

`[string1]\0[string2]\0`

I figured I'd be able to separate them once they arrive, using the '\0' to figure out where to split them. I'm using a function to read just the string and it sort of works, but I keep getting complaints from Valgrind, and I don't understand why. 

I'm going to use an example where only 1 string is read from the buffer, but I mention the strategy because I'm not able to just put the message into a `char* buffer`. I need the function to extract a string from more complex buffers.

It all starts like this: 

```
void* buffer = malloc(msgSize * sizeof(char)); //the message size is properly calculated to include the '\0' at the end
char* instanceId = malloc(msgSize * sizeof(char));

if(recv(socket_desc, (void*) buffer, msgSize * sizeof(char), MSG_WAITALL) <= 0) {
        log_error(logger, "Message failed.");
        return;
    }

    bufferToString(buffer, &instanceId, 0);
    bufferToString2(buffer, instanceId, 0);

```

I made several attempts to make bufferToString work, as you can see... Of course I don't invoke them all at the same time, but I want to share those lines in case I'm making a mistake there. 

**Attempt #Number 1: char by char**

```
int bufferToString(void* buffer, char** string, int startPtr) {
    //startPtr can be used to read strings that are in the middle of a buffer
    char a;
    int thisStringPtr = 0; 

    do {
        a = *(char*) (buffer + startPtr);
        (*string)[thisStringPtr] = a;
        startPtr++;
        thisStringPtr++;
    } while (a != '\0');
    return startPtr; //return end position to use for extracting more values later

```

}

This one complains:

```
==23047== Invalid read of size 1
==23047==    at 0x403A27A: bufferToString (buffer.c:16)
==23047==    by 0x804A0C2: handleHiloInstancia (coordinador.c:232)
==23047==    by 0x8049C54: procesarConexion (coordinador.c:85)
==23047==    by 0x4066294: start_thread (pthread_create.c:333)
==23047==    by 0x41650AD: clone (clone.S:114)
==23047==  Address 0x423bc8a is 0 bytes after a block of size 10 alloc'd
==23047==    at 0x402C17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==23047==    by 0x804A063: handleHiloInstancia (coordinador.c:225)
==23047==    by 0x8049C54: procesarConexion (coordinador.c:85)
==23047==    by 0x4066294: start_thread (pthread_create.c:333)
==23047==    by 0x41650AD: clone (clone.S:114)

```

Line 16 of bufferToString is the first line inside the `do` statement. 

**Attempt 2: cast and copy**

```
int bufferToString2(void* buffer, char* string, int startPtr) {
    strcpy(string, (char*) (buffer + startPtr));
    return (strlen(string) + 1)*sizeof(char);
}

```

With or without the +startPtr, this causes slightly different problems: 

```
==23190== Invalid read of size 1
==23190==    at 0x402F489: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==23190==    by 0x403A1E3: bufferToString2 (buffer.c:3)
==23190==    by 0x804A0C1: handleHiloInstancia (coordinador.c:232)
==23190==    by 0x8049C54: procesarConexion (coordinador.c:85)
==23190==    by 0x4066294: start_thread (pthread_create.c:333)
==23190==    by 0x41650AD: clone (clone.S:114)
==23190==  Address 0x423bc8a is 0 bytes after a block of size 10 alloc'd

```

I tried a few other combinations (like using char\*\* string and all the required modifications in bufferToString2), but I keep getting similar error messages. What am I not seeing?

UPDATE: How message is being sent: 

```
    int bufferSize;
    void* buffer = serializePackage(HANDSHAKE_INSTANCE_ID ,instancia_config->nombre, &bufferSize );
    printf("Buffer size: %i - Instancia Name = %s - Socket num: %i\n", bufferSize, instancia_config->nombre, socket_coordinador); //this shows right data
    if (send(socket_coordinador,buffer,bufferSize, 0) <= 0) {
        log_error(logger, "Could not send ID.");
        endProcess(EXIT_FAILURE);
    }

```

instancia\_config->nombre is of type char\*

```
void* serializePackage(int codigo,char * mensaje, int* tamanioPaquete){

    int puntero = 0;
    int length = strlen(mensaje);

    int sizeOfPaquete = strlen(mensaje) * sizeof(char) + 1 + 2 * sizeof(int);
    void * paquete = malloc(sizeOfPaquete);

    memcpy((paquete + puntero) ,&codigo,sizeof(int));
    puntero += sizeof(int);

    memcpy((paquete + puntero),&length,sizeof(int));
    puntero += sizeof(int);

    memcpy((paquete + puntero),mensaje,length * sizeof(char) + 1);

    *tamanioPaquete = sizeOfPaquete;
    return paquete;

}

```