View Full Version : Linux abstract socket: NULL vs \0

January 1st, 2013, 07:51 PM

I create a Linux abstract socket this way:

static const char *UFBIO_LISTEN_PATH = "/ufbio/port";
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(&addr.sun_path[1], UFBIO_LISTEN_PATH, sizeof(addr.sun_path)-2);
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
//bind(socket...) works fine
and bind() works fine (doesn't collide with sockets from other apps, read below).
But when I make the path start with zero directly:

static const char *UFBIO_LISTEN_PATH = "\0/ufbio/port";
strncpy(addr.sun_path, UFBIO_LISTEN_PATH, sizeof(addr.sun_path)-1);

I get an error "address already in use" during bind() if any other app is running with a socket created this way (even with a different path like "\0another/app/port").

What am I doing wrong with this second approach? Is it the path? - Doesn't the first byte result in zero in both cases? - '\0' and memset(.., 0, ..)

January 1st, 2013, 09:38 PM

static const char *UFBIO_LISTEN_PATH = "\0/ufbio/port";

is the empty string. When you do strncpy(), only the leading 0 byte is written, nothing else.

January 1st, 2013, 09:57 PM
That's a neat subtle catch, thanks!