$OpenBSD: patch-bitlbee_c,v 1.3 2006/04/11 15:11:33 naddy Exp $
--- bitlbee.c.orig	Sun Apr  2 04:53:38 2006
+++ bitlbee.c	Thu Apr  6 23:39:32 2006
@@ -35,7 +35,7 @@
 
 gboolean bitlbee_io_new_client( GIOChannel *source, GIOCondition condition, gpointer data )
 {
-	size_t size = sizeof( struct sockaddr_in );
+	socklen_t size = sizeof( struct sockaddr_in );
 	struct sockaddr_in conn_info;
 	int new_socket = accept( global.listen_socket, (struct sockaddr *) &conn_info, 
 		                     &size );
@@ -154,8 +154,9 @@ gboolean bitlbee_io_current_client_read(
 	}
 	else 
 	{
-		irc->readbuffer = g_renew( char, irc->readbuffer, strlen( irc->readbuffer ) + strlen ( line ) + 1 );
-		strcpy( ( irc->readbuffer + strlen( irc->readbuffer ) ), line );
+		size_t newlen = strlen(irc->readbuffer) + strlen(line) + 1;
+		irc->readbuffer = g_renew( char, irc->readbuffer, newlen);
+		strlcat(irc->readbuffer, line, newlen);
 	}
 	
 	if( !irc_process( irc ) )
@@ -228,10 +229,11 @@ int bitlbee_load( irc_t *irc, const char
 	if( irc->status == USTATUS_IDENTIFIED )
 		return( 1 );
 	
-	g_snprintf( s, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
+	g_snprintf( s, sizeof(s), "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
    	fp = fopen( s, "r" );
    	if( !fp ) return( 0 );
 	
+	COMPILE_TIME_ASSERT(32 < sizeof(s));
 	fscanf( fp, "%32[^\n]s", s );
 	if( setpass( irc, password, s ) < 0 )
 	{
@@ -243,6 +245,7 @@ int bitlbee_load( irc_t *irc, const char
 	   account command will not work otherwise. */
 	irc->status = USTATUS_IDENTIFIED;
 	
+	COMPILE_TIME_ASSERT(511 < sizeof(s));
 	while( fscanf( fp, "%511[^\n]s", s ) > 0 )
 	{
 		fgetc( fp );
@@ -252,19 +255,21 @@ int bitlbee_load( irc_t *irc, const char
 	}
 	fclose( fp );
 	
-	g_snprintf( s, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
+	g_snprintf( s, sizeof(s), "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
 	fp = fopen( s, "r" );
 	if( !fp ) return( 0 );
-	while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 )
+	COMPILE_TIME_ASSERT(511 < sizeof(s));
+	COMPILE_TIME_ASSERT(24 < sizeof(nick));
+	while( fscanf( fp, "%511s %d %24s", s, &proto, nick ) > 0 )
 	{
-		http_decode( s );
+		http_decode( s, sizeof(s) );
 		nick_set( irc, s, proto, nick );
 	}
 	fclose( fp );
 	
 	if( set_getint( irc, "auto_connect" ) )
 	{
-		strcpy( s, "account on" );	/* Can't do this directly because r_c_s alters the string */
+		strlcpy( s, "account on", sizeof(s) );	/* Can't do this directly because r_c_s alters the string */
 		root_command_string( irc, ru, s, 0 );
 	}
 	
@@ -305,15 +310,15 @@ int bitlbee_save( irc_t *irc )
 		return( 0 );
 	}
 	
-	g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" );
+	g_snprintf( path, sizeof(s), "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" );
 	fp = fopen( path, "w" );
 	if( !fp ) return( 0 );
 	for( n = irc->nicks; n; n = n->next )
 	{
-		strcpy( s, n->handle );
-		s[169] = 0; /* Prevent any overflow (169 ~ 512 / 3) */
-		http_encode( s );
-		g_snprintf( s + strlen( s ), 510 - strlen( s ), " %d %s", n->proto, n->nick );
+		strlcpy( s, n->handle, sizeof(s) );
+		s[sizeof(s)/3] = 0; /* Prevent any overflow (169 ~ 512 / 3) */
+		http_encode( s, sizeof(s) );
+		g_snprintf( s + strlen( s ), sizeof(s) - strlen( s ), " %d %s", n->proto, n->nick );
 		if( fprintf( fp, "%s\n", s ) != strlen( s ) + 1 )
 		{
 			irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
@@ -327,7 +332,7 @@ int bitlbee_save( irc_t *irc )
 		return( 0 );
 	}
   
-	g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
+	g_snprintf( new_path, sizeof(new_path), "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
 	if( unlink( new_path ) != 0 )
 	{
 		if( errno != ENOENT )
@@ -342,7 +347,7 @@ int bitlbee_save( irc_t *irc )
 		return( 0 );
 	}
 	
-	g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" );
+	g_snprintf( path, sizeof(path), "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" );
 	fp = fopen( path, "w" );
 	if( !fp ) return( 0 );
 	if( fprintf( fp, "%s", hash ) != strlen( hash ) )
@@ -414,7 +419,7 @@ int bitlbee_save( irc_t *irc )
 		return( 0 );
 	}
 	
- 	g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
+ 	g_snprintf( new_path, sizeof(new_path), "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
  	if( unlink( new_path ) != 0 )
 	{
 		if( errno != ENOENT )
