Skip to content

Commit d1b97ed

Browse files
authored
Merge pull request #83 from AliceLR/fix-pat-stringop-truncation
Fix -Wstringop-truncation warnings in load_pat.cpp
2 parents 234b65d + 83872bb commit d1b97ed

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/load_pat.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ void pat_init_patnames(void)
375375
strcat(pathforpat, "/instruments");
376376
}
377377
strncpy(cfgsources[0], timiditycfg, PATH_MAX - 1);
378+
cfgsources[0][PATH_MAX - 1] = '\0';
378379
nsources = 1;
379380

380381
for( i=0; i<MAXSMP; i++ ) midipat[i][0] = '\0';
@@ -1013,8 +1014,6 @@ static void pat_setpat_inst(WaveHeader *hw, INSTRUMENTHEADER *d, int smp)
10131014
static void PATinst(INSTRUMENTHEADER *d, int smp, int gm)
10141015
{
10151016
WaveHeader hw;
1016-
char s[32];
1017-
memset(s,0,32);
10181017
if( pat_readpat_attr(gm-1, &hw, 0) ) {
10191018
pat_setpat_inst(&hw, d, smp);
10201019
}
@@ -1040,17 +1039,12 @@ static void PATinst(INSTRUMENTHEADER *d, int smp, int gm)
10401039
hw.reserved[sizeof(hw.reserved) - 1] = '\0';
10411040
pat_setpat_inst(&hw, d, smp);
10421041
}
1043-
if( hw.reserved[0] )
1044-
strncpy(s, hw.reserved, 32);
1045-
else
1046-
strncpy(s, midipat[gm-1], 32);
1047-
s[31] = '\0';
1048-
memset(d->name, 0, 32);
1049-
strcpy((char *)d->name, s);
1050-
strncpy(s, midipat[gm-1], 12);
1051-
s[11] = '\0';
1052-
memset(d->filename, 0, 12);
1053-
strcpy((char *)d->filename, s);
1042+
/* strncpy 0-inits the entire field. */
1043+
strncpy((char *)d->name, hw.reserved[0] ? hw.reserved : midipat[gm-1], 32);
1044+
d->name[31] = '\0';
1045+
1046+
strncpy((char *)d->filename, midipat[gm-1], 12);
1047+
d->filename[11] = '\0';
10541048
}
10551049

10561050
static void pat_setpat_attr(WaveHeader *hw, MODINSTRUMENT *q)
@@ -1077,11 +1071,18 @@ static void pat_setpat_attr(WaveHeader *hw, MODINSTRUMENT *q)
10771071
static void PATsample(CSoundFile *cs, MODINSTRUMENT *q, int smp, int gm)
10781072
{
10791073
WaveHeader hw;
1080-
char s[256];
1074+
char s[PATH_MAX + 32];
10811075
sprintf(s, "%d:%s", smp-1, midipat[gm-1]);
10821076
s[31] = '\0';
1083-
memset(cs->m_szNames[smp], 0, 32);
1084-
strncpy(cs->m_szNames[smp], s, 32-1);
1077+
1078+
#if defined(__GNUC__) && __GNUC__ >= 8
1079+
/* GCC's warning is broken and ignores the manual termination in this case. */
1080+
#pragma GCC diagnostic ignored "-Wstringop-truncation"
1081+
#endif
1082+
/* strncpy 0-inits the entire field. */
1083+
strncpy(cs->m_szNames[smp], s, 32);
1084+
cs->m_szNames[smp][31] = '\0';
1085+
10851086
q->nGlobalVol = 64;
10861087
q->nPan = 128;
10871088
q->uFlags = CHN_16BIT;

0 commit comments

Comments
 (0)