strlcpy vs strcpy - much difference?

Posted by Ked on Mon 09 Jun 2008 10:00 AM — 5 posts, 26,576 views.

Russia #0
Hi there!

I am trying to get Unix sockets to work with Lua on Linux and since Lua seems to have no direct support for those, and nor does LuaSocket, and nor do I know enough C to add them in myself, I had to go and google up someone else's code (no small task, as it turned out).

The code I did manage to find eventually seems to work, but there's one thing that bugs me. The original file had the following line in it:

strlcpy(addr.sun_path, socket_filename, sizeof(addr.sun_path));


But this function is missing from Linux, so I used the simplest workaround I could think of - replacing it with:

strcpy(addr.sun_path, socket_filename);


So my question is: am I in trouble and if yes, then how big is this trouble and is there any better way to go about fixing this.

Here's the complete function in question for reference:


static int
socket_unix_server(lua_State *L) /* path */
{
	assert(L);

	const char *socket_filename = luaL_checkstring(L, 1);

        /* make sure we don't overwrite a regular file */       
        struct stat st;
        if(lstat(socket_filename, &st) == 0)
        {
                if(S_ISREG(st.st_mode))
                {
			return luaL_error(L, "file already exists and is not a socket");
                }
        }

	struct sockaddr_un addr;
	bzero(&addr, sizeof(addr));

	int fd = socket(PF_UNIX, SOCK_STREAM, 0);
	if(fd == -1)
		return luaL_error(L, "%s", strerror(errno));

        if(unlink(socket_filename) != 0 && errno != ENOENT)
		return luaL_error(L, "%s", strerror(errno));

        addr.sun_family = AF_UNIX;
        //strlcpy(addr.sun_path, socket_filename, sizeof(addr.sun_path));
        strcpy(addr.sun_path, socket_filename);
        if(bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) != 0)
		return luaL_error(L, "%s", strerror(errno));

	int rc = listen(fd, 20);
	if(rc != 0)
		return luaL_error(L, "%s", strerror(errno));

	lua_pushinteger(L, fd);
	return 1;
}
Netherlands #1
I'm not so well-known in C, but some stuff google turned up:

http://www.ussg.iu.edu/hypermail/linux/kernel/0311.3/0820.html

http://udel.edu/~pconrad/UnixAtUD/strcpy.html


An (innocent) glance at the latter article seems to suggest you use the following to ensure a trailing \0 no matter what:

strncpy(dest,src,LEN);
dest[LEN-1]='\0'; /* null terminate for safety */
Australia Forum Administrator #2
What are you trying to do here exactly? Luasocket uses AF_UNIX but you want to use PF_UNIX, is that right? Is this for local interprocess communication? This is not for a MUD server then, because that is hardly "local".

Anyway, as to strlcpy (which I think I have never used) a quick browse seems to indicate it works more "sensibly" than strncpy, which can leave out the terminating nul byte if the destination is exactly full.

Hence the suggestion of using strncpy with a replacement of the final byte by a nul.
USA #3
strlcpy() is usually found on BSD systems, but can sometimes be found elsewhere if the system's SSH libraries include it. It's where the mudstrlcpy function that's found in FUSS came from actually.
Russia #4
Thanks everyone, I'll replace it with strncpy.

And, Nick, this is for an interface to Kmuddy - I tried to use it on Linux but found support for Lua a little lacking (or rather virtually non-existent), so decided to see if I can get it to work properly on my own. User scripts communicate with the client via a unix socket and the client talks to them through TCP ones. LuaSocket takes care of the TCP side of things, but not unix.