[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  strlcpy vs strcpy - much difference?

strlcpy vs strcpy - much difference?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Mon 09 Jun 2008 10:00 AM (UTC)
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 */

	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)
			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;
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] bio
Date Reply #1 on Mon 09 Jun 2008 04:49 PM (UTC)
I'm not so well-known in C, but some stuff google turned up:



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

dest[LEN-1]='\0'; /* null terminate for safety */
[Go to top] top

Posted by Nick Gammon   Australia  (23,016 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Mon 09 Jun 2008 10:47 PM (UTC)
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Samson   USA  (683 posts)  [Biography] bio
Date Reply #3 on Tue 10 Jun 2008 12:17 AM (UTC)
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.
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Reply #4 on Tue 10 Jun 2008 03:19 PM (UTC)
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.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]