Handle snprintf() format-truncation warnings

This commit is contained in:
Marcel Waldvogel 2019-01-02 22:13:18 +01:00 committed by Erik de Castro Lopo
parent bef2abc9e8
commit c0dd8b4a4f
4 changed files with 39 additions and 25 deletions

View File

@ -116,8 +116,13 @@ main (int argc, char **argv)
for (ch = 0 ; ch < state.channels ; ch++)
{ char filename [520] ;
size_t count ;
snprintf (filename, sizeof (filename), "%s_%02d%s", pathname, ch, ext) ;
count = snprintf (filename, sizeof (filename), "%s_%02d%s", pathname, ch, ext) ;
if (count >= sizeof (filename))
{ printf ("File name truncated to %s\n", filename) ;
} ;
if ((state.outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
{ printf ("Not able to open output file '%s'\n%s\n", filename, sf_strerror (NULL)) ;

View File

@ -219,6 +219,7 @@ psf_save_write_chunk (WRITE_CHUNKS * pchk, const SF_CHUNK_INFO * chunk_info)
{ union
{ uint32_t marker ;
char str [5] ;
/* Update snprintf() format string below when changing this */
} u ;
uint32_t len ;
@ -241,7 +242,7 @@ psf_save_write_chunk (WRITE_CHUNKS * pchk, const SF_CHUNK_INFO * chunk_info)
len = chunk_info->datalen ;
while (len & 3) len ++ ;
snprintf (u.str, sizeof (u.str), "%s", chunk_info->id) ;
snprintf (u.str, sizeof (u.str), "%.4s", chunk_info->id) ;
pchk->chunks [pchk->used].hash = strlen (chunk_info->id) > 4 ? hash_of_str (chunk_info->id) : u.marker ;
pchk->chunks [pchk->used].mark32 = u.marker ;

View File

@ -116,33 +116,36 @@ psf_fclose (SF_PRIVATE *psf)
int
psf_open_rsrc (SF_PRIVATE *psf)
{
{ size_t count ;
if (psf->rsrc.filedes > 0)
return 0 ;
/* Test for MacOSX style resource fork on HPFS or HPFS+ filesystems. */
snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s/..namedfork/rsrc", psf->file.path.c) ;
count = snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s/..namedfork/rsrc", psf->file.path.c) ;
psf->error = SFE_NO_ERROR ;
if ((psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0)
{ psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ;
if (psf->rsrclength > 0 || (psf->rsrc.mode & SFM_WRITE))
return SFE_NO_ERROR ;
psf_close_fd (psf->rsrc.filedes) ;
psf->rsrc.filedes = -1 ;
} ;
if (count < sizeof (psf->rsrc.path.c))
{ if ((psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0)
{ psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ;
if (psf->rsrclength > 0 || (psf->rsrc.mode & SFM_WRITE))
return SFE_NO_ERROR ;
psf_close_fd (psf->rsrc.filedes) ;
psf->rsrc.filedes = -1 ;
} ;
if (psf->rsrc.filedes == - SFE_BAD_OPEN_MODE)
{ psf->error = SFE_BAD_OPEN_MODE ;
return psf->error ;
if (psf->rsrc.filedes == - SFE_BAD_OPEN_MODE)
{ psf->error = SFE_BAD_OPEN_MODE ;
return psf->error ;
} ;
} ;
/*
** Now try for a resource fork stored as a separate file in the same
** directory, but preceded with a dot underscore.
*/
snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s._%s", psf->file.dir.c, psf->file.name.c) ;
count = snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s._%s", psf->file.dir.c, psf->file.name.c) ;
psf->error = SFE_NO_ERROR ;
if ((psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0)
if (count < sizeof (psf->rsrc.path.c) && (psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0)
{ psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ;
return SFE_NO_ERROR ;
} ;
@ -151,16 +154,21 @@ psf_open_rsrc (SF_PRIVATE *psf)
** Now try for a resource fork stored in a separate file in the
** .AppleDouble/ directory.
*/
snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s.AppleDouble/%s", psf->file.dir.c, psf->file.name.c) ;
count = snprintf (psf->rsrc.path.c, sizeof (psf->rsrc.path.c), "%s.AppleDouble/%s", psf->file.dir.c, psf->file.name.c) ;
psf->error = SFE_NO_ERROR ;
if ((psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0)
{ psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ;
return SFE_NO_ERROR ;
} ;
if (count < sizeof (psf->rsrc.path.c))
{ if ((psf->rsrc.filedes = psf_open_fd (&psf->rsrc)) >= 0)
{ psf->rsrclength = psf_get_filelen_fd (psf->rsrc.filedes) ;
return SFE_NO_ERROR ;
} ;
/* No resource file found. */
if (psf->rsrc.filedes == -1)
psf_log_syserr (psf, errno) ;
/* No resource file found. */
if (psf->rsrc.filedes == -1)
psf_log_syserr (psf, errno) ;
}
else
{ psf->error = SFE_OPEN_FAILED ;
} ;
psf->rsrc.filedes = -1 ;

View File

@ -131,7 +131,7 @@ mpc2k_write_header (SF_PRIVATE *psf, int calc_length)
if (psf->is_pipe == SF_FALSE)
psf_fseek (psf, 0, SEEK_SET) ;
snprintf (sample_name, sizeof (sample_name), "%s ", psf->file.name.c) ;
snprintf (sample_name, sizeof (sample_name), "%-*.*s", HEADER_NAME_LEN, HEADER_NAME_LEN, psf->file.name.c) ;
psf_binheader_writef (psf, "e11b", BHW1 (1), BHW1 (4), BHWv (sample_name), BHWz (HEADER_NAME_LEN)) ;
psf_binheader_writef (psf, "e111", BHW1 (100), BHW1 (0), BHW1 ((psf->sf.channels - 1) & 1)) ;