Search code examples
cstringmultithreadingtokenize

Thread-safe split ip:port in c


I have a text containing a list of ip:port and I have a multi-threading program that splits them to IP and port separately I used strtok but it gives a lot of errors and problems and makes the program gets segmentation fault every time + it needs make to strdup char pointer which very annoying I used strchr but it only gives me the port but can't use it to extract the IP the goal is extract IP as a string and the port as integer and never gets segmentation failed whatever

example:

This is the start function of the thread

void* startthread() {
    do {
        char* proxy = "127.0.0.1:443";

        char* buffered = strdup(proxy);

        char* host = strtok(buffered,":");
        int port = atoi(strtok(NULL,":"));

        printf("%s:%d",host,port);
    }while(1);
}

gdb

Thread 8 "main" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff4a34700 (LWP 47410)]
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0,


Solution

  • One fairly simple way to handle this (that, in keeping with your tags) is compatible with both C and C++) would be to use sscanf:

    char host[256];
    int port;
    
    sscanf(proxy, "%255[^:]:%d", host, &port);
    

    But yes, strtok and multithreading is a lousy combination. Then agin, strtok and almost anything is a lousy combination.

    I suppose to be complete, I need to point out that that the [^:] scan format depends on implementation defined behavior. In theory, an implementation is allowed to treat this literally--as meaning either ^ or :, rather than "anything except a colon"--but I know of only one implementation ever (Borland's compilers for MS-DOS) that did so, and it's long-since obsolete.