Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ 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 page


Posted by Ked   Russia  (524 posts)  Bio
Date Mon 09 Jun 2008 10:00 AM (UTC)
Message
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;
}
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #1 on Mon 09 Jun 2008 04:49 PM (UTC)
Message
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 */
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Mon 09 Jun 2008 10:47 PM (UTC)
Message
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
Top

Posted by Samson   USA  (683 posts)  Bio
Date Reply #3 on Tue 10 Jun 2008 12:17 AM (UTC)
Message
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.
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #4 on Tue 10 Jun 2008 03:19 PM (UTC)
Message
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.
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.


21,225 views.

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

Go to topic:           Search the forum


[Go to top] top

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