Skip to content

Commit 83872bb

Browse files
committed
Fix -Wstringop-truncation warnings in load_pat.cpp
1 parent 8d0b03a commit 83872bb

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
@@ -372,6 +372,7 @@ void pat_init_patnames(void)
372372
strcat(pathforpat, "/instruments");
373373
}
374374
strncpy(cfgsources[0], timiditycfg, PATH_MAX - 1);
375+
cfgsources[0][PATH_MAX - 1] = '\0';
375376
nsources = 1;
376377

377378
for( i=0; i<MAXSMP; i++ ) midipat[i][0] = '\0';
@@ -993,8 +994,6 @@ static void pat_setpat_inst(WaveHeader *hw, INSTRUMENTHEADER *d, int smp)
993994
static void PATinst(INSTRUMENTHEADER *d, int smp, int gm)
994995
{
995996
WaveHeader hw;
996-
char s[32];
997-
memset(s,0,32);
998997
if( pat_readpat_attr(gm-1, &hw, 0) ) {
999998
pat_setpat_inst(&hw, d, smp);
1000999
}
@@ -1020,17 +1019,12 @@ static void PATinst(INSTRUMENTHEADER *d, int smp, int gm)
10201019
hw.reserved[sizeof(hw.reserved) - 1] = '\0';
10211020
pat_setpat_inst(&hw, d, smp);
10221021
}
1023-
if( hw.reserved[0] )
1024-
strncpy(s, hw.reserved, 32);
1025-
else
1026-
strncpy(s, midipat[gm-1], 32);
1027-
s[31] = '\0';
1028-
memset(d->name, 0, 32);
1029-
strcpy((char *)d->name, s);
1030-
strncpy(s, midipat[gm-1], 12);
1031-
s[11] = '\0';
1032-
memset(d->filename, 0, 12);
1033-
strcpy((char *)d->filename, s);
1022+
/* strncpy 0-inits the entire field. */
1023+
strncpy((char *)d->name, hw.reserved[0] ? hw.reserved : midipat[gm-1], 32);
1024+
d->name[31] = '\0';
1025+
1026+
strncpy((char *)d->filename, midipat[gm-1], 12);
1027+
d->filename[11] = '\0';
10341028
}
10351029

10361030
static void pat_setpat_attr(WaveHeader *hw, MODINSTRUMENT *q)
@@ -1057,11 +1051,18 @@ static void pat_setpat_attr(WaveHeader *hw, MODINSTRUMENT *q)
10571051
static void PATsample(CSoundFile *cs, MODINSTRUMENT *q, int smp, int gm)
10581052
{
10591053
WaveHeader hw;
1060-
char s[256];
1054+
char s[PATH_MAX + 32];
10611055
sprintf(s, "%d:%s", smp-1, midipat[gm-1]);
10621056
s[31] = '\0';
1063-
memset(cs->m_szNames[smp], 0, 32);
1064-
strncpy(cs->m_szNames[smp], s, 32-1);
1057+
1058+
#if defined(__GNUC__) && __GNUC__ >= 8
1059+
/* GCC's warning is broken and ignores the manual termination in this case. */
1060+
#pragma GCC diagnostic ignored "-Wstringop-truncation"
1061+
#endif
1062+
/* strncpy 0-inits the entire field. */
1063+
strncpy(cs->m_szNames[smp], s, 32);
1064+
cs->m_szNames[smp][31] = '\0';
1065+
10651066
q->nGlobalVol = 64;
10661067
q->nPan = 128;
10671068
q->uFlags = CHN_16BIT;

0 commit comments

Comments
 (0)