Index: lib/ftutil.c
===================================================================
--- lib/ftutil.c	(revision e8c8d00ea43c204ee276bde7fb663a0f0249790f)
+++ lib/ftutil.c	(revision 60e4df367e5c3af0eb1aada19f9c39ef7079e8e6)
@@ -28,17 +28,7 @@
 #include "lib/ftutil.h"
 
-/* Some ifdefs for ulibc and apparently also BSD (Thanks to Whoopie) */
-#ifndef HOST_NAME_MAX
-#include <sys/param.h>
-#ifdef MAXHOSTNAMELEN
-#define HOST_NAME_MAX MAXHOSTNAMELEN
-#else
-#define HOST_NAME_MAX 255
-#endif
-#endif
-
 #define ASSERTSOCKOP(op, msg) \
 	if( (op) == -1 ) {\
-		sprintf( errmsg , msg ": %s", strerror( errno ) ); \
+		g_snprintf( errmsg, sizeof( errmsg ), msg ": %s", strerror( errno ) ); \
 		return -1; }
 
@@ -46,17 +36,17 @@
  * Creates a listening socket and returns it in saddr_ptr.
  */
-int ft_listen( struct sockaddr_storage *saddr_ptr, char *hostp, char *port, int for_bitlbee_client, char **errptr )
+int ft_listen( struct sockaddr_storage *saddr_ptr, char *host, char *port, int for_bitlbee_client, char **errptr )
 {
-	int fd,gret,saddrlen;
+	int fd, gret, saddrlen;
 	struct addrinfo hints, *rp;
 	socklen_t ssize = sizeof( struct sockaddr_storage );
 	struct sockaddr_storage saddrs, *saddr = &saddrs;
 	static char errmsg[1024];
-	char host[ HOST_NAME_MAX + 1 ];
 	char *ftlisten = global.conf->ft_listen;
 
-	*errptr = errmsg;
+	if( errptr )
+		*errptr = errmsg;
 
-	sprintf( port, "0" );
+	strcpy( port, "0" );
 
 	/* Format is <IP-A>[:<Port-A>];<IP-B>[:<Port-B>] where
@@ -64,25 +54,35 @@
 	 * and B is for connections with IM peers.
 	 */
-	if( ftlisten ) {
+	if( ftlisten )
+	{
 		char *scolon = strchr( ftlisten, ';' );
 		char *colon;
 
-		if( scolon ) {
-			if( for_bitlbee_client ) {
+		if( scolon )
+		{
+			if( for_bitlbee_client )
+			{
 				*scolon = '\0';
-				sprintf( host, ftlisten );
+				strncpy( host, ftlisten, HOST_NAME_MAX );
 				*scolon = ';';
-			} else {
-				sprintf( host, scolon + 1 );
 			}
-		} else {
-			sprintf( host, ftlisten );
+			else
+			{
+				strncpy( host, scolon + 1, HOST_NAME_MAX );
+			}
+		}
+		else
+		{
+			strncpy( host, ftlisten, HOST_NAME_MAX );
 		}
 
-		if( ( colon = strchr( host, ':' ) ) ) {
+		if( ( colon = strchr( host, ':' ) ) )
+		{
 			*colon = '\0';
-			sprintf( port, colon + 1 );
+			strncpy( port, colon + 1, 5 );
 		}
-	} else {
+	}
+	else
+	{
 		ASSERTSOCKOP( gethostname( host, HOST_NAME_MAX + 1 ), "gethostname()" );
 	}
@@ -92,5 +92,6 @@
 	hints.ai_flags = AI_NUMERICSERV;
 
-	if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) != 0 ) {
+	if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) != 0 )
+	{
 		sprintf( errmsg, "getaddrinfo() failed: %s", gai_strerror( gret ) );
 		return -1;
@@ -104,13 +105,13 @@
 
 	ASSERTSOCKOP( fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening socket" );
-
 	ASSERTSOCKOP( bind( fd, ( struct sockaddr *)saddr, saddrlen ), "Binding socket" );
-	
 	ASSERTSOCKOP( listen( fd, 1 ), "Making socket listen" );
 
 	if ( !inet_ntop( saddr->ss_family, saddr->ss_family == AF_INET ?
-			( void * )&( ( struct sockaddr_in * ) saddr )->sin_addr.s_addr : ( void * )&( ( struct sockaddr_in6 * ) saddr )->sin6_addr.s6_addr
-			, host, INET6_ADDRSTRLEN ) ) {
-		sprintf( errmsg, "inet_ntop failed on listening socket" );
+			( void * )&( ( struct sockaddr_in * ) saddr )->sin_addr.s_addr :
+			( void * )&( ( struct sockaddr_in6 * ) saddr )->sin6_addr.s6_addr,
+			host, HOST_NAME_MAX ) )
+	{
+		strcpy( errmsg, "inet_ntop failed on listening socket" );
 		return -1;
 	}
@@ -119,13 +120,15 @@
 
 	if( saddr->ss_family == AF_INET )
-		sprintf( port, "%d", ntohs( ( ( struct sockaddr_in *) saddr )->sin_port ) );
+		g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in *) saddr )->sin_port ) );
 	else
-		sprintf( port, "%d", ntohs( ( ( struct sockaddr_in6 *) saddr )->sin6_port ) );
+		g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in6 *) saddr )->sin6_port ) );
 
 	if( saddr_ptr )
 		memcpy( saddr_ptr, saddr, saddrlen );
 
-	strcpy( hostp, host );
-
+	/* I hate static-length strings.. */
+	host[HOST_NAME_MAX] = '\0';
+	port[5] = '\0';
+	
 	return fd;
 }
Index: lib/ftutil.h
===================================================================
--- lib/ftutil.h	(revision e8c8d00ea43c204ee276bde7fb663a0f0249790f)
+++ lib/ftutil.h	(revision 60e4df367e5c3af0eb1aada19f9c39ef7079e8e6)
@@ -26,3 +26,15 @@
 #endif
 
+/* Some ifdefs for ulibc and apparently also BSD (Thanks to Whoopie) */
+#ifndef HOST_NAME_MAX
+#include <sys/param.h>
+#ifdef MAXHOSTNAMELEN
+#define HOST_NAME_MAX MAXHOSTNAMELEN
+#else
+#define HOST_NAME_MAX 255
+#endif
+#endif
+
+/* This function should be used with care. host should be AT LEAST a
+   char[HOST_NAME_MAX+1] and port AT LEAST a char[6]. */
 int ft_listen( struct sockaddr_storage *saddr_ptr, char *host, char *port, int for_bitlbee_client, char **errptr );
