Skip to content

Commit

Permalink
Merge pull request #83 from AliceLR/fix-pat-stringop-truncation
Browse files Browse the repository at this point in the history
Fix -Wstringop-truncation warnings in load_pat.cpp
  • Loading branch information
Konstanty authored Jan 30, 2022
2 parents 234b65d + 83872bb commit d1b97ed
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/load_pat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ void pat_init_patnames(void)
strcat(pathforpat, "/instruments");
}
strncpy(cfgsources[0], timiditycfg, PATH_MAX - 1);
cfgsources[0][PATH_MAX - 1] = '\0';
nsources = 1;

for( i=0; i<MAXSMP; i++ ) midipat[i][0] = '\0';
Expand Down Expand Up @@ -1013,8 +1014,6 @@ static void pat_setpat_inst(WaveHeader *hw, INSTRUMENTHEADER *d, int smp)
static void PATinst(INSTRUMENTHEADER *d, int smp, int gm)
{
WaveHeader hw;
char s[32];
memset(s,0,32);
if( pat_readpat_attr(gm-1, &hw, 0) ) {
pat_setpat_inst(&hw, d, smp);
}
Expand All @@ -1040,17 +1039,12 @@ static void PATinst(INSTRUMENTHEADER *d, int smp, int gm)
hw.reserved[sizeof(hw.reserved) - 1] = '\0';
pat_setpat_inst(&hw, d, smp);
}
if( hw.reserved[0] )
strncpy(s, hw.reserved, 32);
else
strncpy(s, midipat[gm-1], 32);
s[31] = '\0';
memset(d->name, 0, 32);
strcpy((char *)d->name, s);
strncpy(s, midipat[gm-1], 12);
s[11] = '\0';
memset(d->filename, 0, 12);
strcpy((char *)d->filename, s);
/* strncpy 0-inits the entire field. */
strncpy((char *)d->name, hw.reserved[0] ? hw.reserved : midipat[gm-1], 32);
d->name[31] = '\0';

strncpy((char *)d->filename, midipat[gm-1], 12);
d->filename[11] = '\0';
}

static void pat_setpat_attr(WaveHeader *hw, MODINSTRUMENT *q)
Expand All @@ -1077,11 +1071,18 @@ static void pat_setpat_attr(WaveHeader *hw, MODINSTRUMENT *q)
static void PATsample(CSoundFile *cs, MODINSTRUMENT *q, int smp, int gm)
{
WaveHeader hw;
char s[256];
char s[PATH_MAX + 32];
sprintf(s, "%d:%s", smp-1, midipat[gm-1]);
s[31] = '\0';
memset(cs->m_szNames[smp], 0, 32);
strncpy(cs->m_szNames[smp], s, 32-1);

#if defined(__GNUC__) && __GNUC__ >= 8
/* GCC's warning is broken and ignores the manual termination in this case. */
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif
/* strncpy 0-inits the entire field. */
strncpy(cs->m_szNames[smp], s, 32);
cs->m_szNames[smp][31] = '\0';

q->nGlobalVol = 64;
q->nPan = 128;
q->uFlags = CHN_16BIT;
Expand Down

0 comments on commit d1b97ed

Please sign in to comment.