src/ : Use functions psf_strlcpy() and psf_strlcat() as appropriate.

This commit is contained in:
Erik de Castro Lopo 2010-10-12 19:50:26 +11:00
parent ca0c4b82a5
commit 43eba45a81
9 changed files with 21 additions and 17 deletions

View File

@ -3,6 +3,10 @@
* src/common.h
Add functions psf_strlcpy() and psf_strlcat().
* src/broadcast.c src/sndfile.c src/strings.c src/test_main.c
src/test_main.h src/test_strncpy_crlf.c
Use functions psf_strlcpy() and psf_strlcat() as appropriate.
2010-10-10 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* programs/*.c

View File

@ -67,18 +67,18 @@ broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datas
memcpy (psf->broadcast_16k, info, offsetof (SF_BROADCAST_INFO, coding_history)) ;
psf_strncpy_crlf (psf->broadcast_16k->coding_history, info->coding_history, sizeof (psf->broadcast_16k->coding_history), datasize - offsetof (SF_BROADCAST_INFO, coding_history)) ;
psf_strlcpy_crlf (psf->broadcast_16k->coding_history, info->coding_history, sizeof (psf->broadcast_16k->coding_history), datasize - offsetof (SF_BROADCAST_INFO, coding_history)) ;
len = strlen (psf->broadcast_16k->coding_history) ;
if (len > 0 && psf->broadcast_16k->coding_history [len - 1] != '\n')
psf_safe_strncat (psf->broadcast_16k->coding_history, "\r\n", sizeof (psf->broadcast_16k->coding_history)) ;
psf_strlcat (psf->broadcast_16k->coding_history, sizeof (psf->broadcast_16k->coding_history), "\r\n") ;
if (psf->file.mode == SFM_WRITE)
{ char added_history [256] ;
size_t added_history_len ;
added_history_len = gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
psf_safe_strncat (psf->broadcast_16k->coding_history, added_history, sizeof (psf->broadcast_16k->coding_history)) ;
psf_strlcat (psf->broadcast_16k->coding_history, sizeof (psf->broadcast_16k->coding_history), added_history) ;
} ;
/* Force coding_history_size to be even. */
@ -138,11 +138,11 @@ gen_coding_history (char * added_history, int added_history_max, const SF_INFO *
return SF_FALSE ;
case 1 :
strncpy (chnstr, "mono", sizeof (chnstr)) ;
psf_strlcpy (chnstr, sizeof (chnstr), "mono") ;
break ;
case 2 :
strncpy (chnstr, "stereo", sizeof (chnstr)) ;
psf_strlcpy (chnstr, sizeof (chnstr), "stereo") ;
break ;
default :

View File

@ -1331,7 +1331,7 @@ append_snprintf (char * dest, size_t maxlen, const char * fmt, ...)
void
psf_strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
psf_strlcpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
{ /* Must be minus 2 so it can still expand a single trailing '\n' or '\r'. */
char * destend = dest + destmax - 2 ;
const char * srcend = src + srcmax ;
@ -1363,7 +1363,7 @@ psf_strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
/* Make sure dest is terminated. */
*dest = 0 ;
} /* psf_strncpy_crlf */
} /* psf_strlcpy_crlf */
/*==============================================================================
*/

View File

@ -664,7 +664,7 @@ void psf_log_SF_INFO (SF_PRIVATE *psf) ;
int32_t psf_rand_int32 (void) ;
void append_snprintf (char * dest, size_t maxlen, const char * fmt, ...) ;
void psf_strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
void psf_strlcpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
/* Functions used when writing file headers. */

View File

@ -2259,7 +2259,7 @@ format_from_extension (SF_PRIVATE *psf)
if (strlen (cptr) > sizeof (buffer) - 1)
return 0 ;
strncpy (buffer, cptr, sizeof (buffer)) ;
psf_strlcpy (buffer, sizeof (buffer), cptr) ;
buffer [sizeof (buffer) - 1] = 0 ;
/* Convert everything in the buffer to lower case. */

View File

@ -110,9 +110,9 @@ psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
*/
if (strstr (str, PACKAGE) == NULL && len_remaining > (int) (strlen (bracket_name) + str_len + 2))
{ if (strlen (str) == 0)
psf_safe_strncat (psf->str_end, lsf_name, len_remaining) ;
psf_strlcat (psf->str_end, sizeof (psf->str_storage), lsf_name) ;
else
psf_safe_strncat (psf->str_end, bracket_name, len_remaining) ;
psf_strlcat (psf->str_end, sizeof (psf->str_storage), bracket_name) ;
psf->str_end += strlen (psf->str_end) ;
} ;

View File

@ -40,7 +40,7 @@ main (void)
test_audio_detect () ;
test_ima_oki_adpcm () ;
test_strncpy_crlf () ;
test_psf_strlcpy_crlf () ;
test_broadcast_var () ;
return 0 ;

View File

@ -35,5 +35,5 @@ void test_double_convert (void) ;
void test_audio_detect (void) ;
void test_ima_oki_adpcm (void) ;
void test_strncpy_crlf (void) ;
void test_psf_strlcpy_crlf (void) ;
void test_broadcast_var (void) ;

View File

@ -26,12 +26,12 @@
#include "test_main.h"
void
test_strncpy_crlf (void)
test_psf_strlcpy_crlf (void)
{ const char *src = "a\nb\nc\n" ;
char *dest ;
int dest_len ;
print_test_name ("Testing psf_strncpy_crlf") ;
print_test_name ("Testing psf_strlcpy_crlf") ;
for (dest_len = 3 ; dest_len < 30 ; dest_len++)
{ dest = calloc (1, dest_len + 1) ;
@ -42,7 +42,7 @@ test_strncpy_crlf (void)
dest [dest_len] = 0xea ;
psf_strncpy_crlf (dest, src, dest_len, sizeof (src)) ;
psf_strlcpy_crlf (dest, src, dest_len, sizeof (src)) ;
if (dest [dest_len] != 0xea)
{ printf ("\n\nLine %d: buffer overrun for dest_len == %d\n\n", __LINE__, dest_len) ;
@ -53,4 +53,4 @@ test_strncpy_crlf (void)
} ;
puts ("ok") ;
} /* test_strncpy_crlf */
} /* test_psf_strlcpy_crlf */