From: <sag...@us...> - 2013-03-25 23:15:09
|
Revision: 1696 http://sourceforge.net/p/modplug/code/1696 Author: saga-games Date: 2013-03-25 23:14:59 +0000 (Mon, 25 Mar 2013) Log Message: ----------- [Ref] Guarantee ModInstrument::name to be null-terminated. Basically, it should have been like that already. [Ref] Added StringFixer::Copy and StringFixer::CopyN, which are idiot-proof strncpy wrappers which always add the trailing null character. [Ref] Got rid of a few more mpt::String members which are already part of std::string (e.g. Empty <-> clear, IsEmpty <-> empty) Modified Paths: -------------- trunk/OpenMPT/common/StringFixer.h trunk/OpenMPT/common/mptString.h trunk/OpenMPT/mptrack/AbstractVstEditor.cpp trunk/OpenMPT/mptrack/CleanupSong.cpp trunk/OpenMPT/mptrack/Ctrl_ins.cpp trunk/OpenMPT/mptrack/Ctrl_pat.cpp trunk/OpenMPT/mptrack/Ctrl_seq.cpp trunk/OpenMPT/mptrack/Ctrl_smp.cpp trunk/OpenMPT/mptrack/Draw_pat.cpp trunk/OpenMPT/mptrack/Moddoc.cpp trunk/OpenMPT/mptrack/Modedit.cpp trunk/OpenMPT/mptrack/Mptrack.cpp trunk/OpenMPT/mptrack/PatternEditorDialogs.cpp trunk/OpenMPT/mptrack/SelectPluginDialog.cpp trunk/OpenMPT/mptrack/View_gen.cpp trunk/OpenMPT/mptrack/View_tre.cpp trunk/OpenMPT/mptrack/Vstplug.cpp trunk/OpenMPT/mptrack/Vstplug.h trunk/OpenMPT/mptrack/dlg_misc.cpp trunk/OpenMPT/mptrack/mod2midi.cpp trunk/OpenMPT/mptrack/view_com.cpp trunk/OpenMPT/soundlib/Dlsbank.cpp trunk/OpenMPT/soundlib/Load_itp.cpp trunk/OpenMPT/soundlib/Load_umx.cpp trunk/OpenMPT/soundlib/Message.cpp trunk/OpenMPT/soundlib/ModInstrument.h trunk/OpenMPT/soundlib/SampleFormats.cpp trunk/OpenMPT/soundlib/Sndfile.cpp trunk/OpenMPT/soundlib/WAVTools.cpp trunk/OpenMPT/soundlib/pattern.cpp trunk/OpenMPT/soundlib/patternContainer.cpp Modified: trunk/OpenMPT/common/StringFixer.h =================================================================== --- trunk/OpenMPT/common/StringFixer.h 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/common/StringFixer.h 2013-03-25 23:14:59 UTC (rev 1696) @@ -215,4 +215,22 @@ } } + + // Copy from one fixed size char array to another one. + template <size_t destSize, size_t srcSize> + void Copy(char (&destBuffer)[destSize], const char (&srcBuffer)[srcSize]) + //----------------------------------------------------------------------- + { + CopyN(destBuffer, srcBuffer, srcSize); + } + + // Copy from a char array to a fixed size char array. + template <size_t destSize> + void CopyN(char (&destBuffer)[destSize], const char *srcBuffer, const size_t srcSize = SIZE_T_MAX) + //------------------------------------------------------------------------------------------------ + { + const size_t copySize = min(destSize - 1, srcSize); + strncpy(destBuffer, srcBuffer, copySize); + destBuffer[copySize] = '\0'; + } }; Modified: trunk/OpenMPT/common/mptString.h =================================================================== --- trunk/OpenMPT/common/mptString.h 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/common/mptString.h 2013-03-25 23:14:59 UTC (rev 1696) @@ -44,32 +44,17 @@ // To allow easy assignment to CString in GUI side. operator const CharT*() const {return c_str();} - // Clears string. - void Empty() {clear();} - - // Tests whether string is empty. - bool IsEmpty() const {return empty();} - - // See std::string::compare. - int Compare(const CharT* psz) const {return compare(psz);} - - // Equivalent to Empty(); Append(psz, nCount); + // Set the string to psz, copy at most nCount characters void SetString(const CharT* psz, const size_t nCount) { - Empty(); + clear(); Append(psz, nCount); } - // Append string to this. - void Append(const CharT* psz) {append(psz);} - // Appends given string to this. Stops after 'nCount' chars if null terminator // hasn't been encountered before that. void Append(const CharT* psz, const size_t nCount) {append(psz, nCount);} - // Appends single character to string. - void AppendChar(const CharT c) {append(1, c);} - // Difference between Append and AppendChars is that latter can be used to add // potentially non-null terminated char array. template <size_t N> @@ -78,9 +63,6 @@ append(arr, arr + N); } - // Reserves store for the string without resizing. - void Reserve(size_t nSize) {reserve(nSize);} - // Formats this string, like CString::Format. void Format(const CharT* pszFormat, ...) { Modified: trunk/OpenMPT/mptrack/AbstractVstEditor.cpp =================================================================== --- trunk/OpenMPT/mptrack/AbstractVstEditor.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/AbstractVstEditor.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -873,7 +873,7 @@ m_nInstrument = nIns; _snprintf(pIns->name, CountOf(pIns->name) - 1, _T("%d: %s"), m_pVstPlugin->GetSlot() + 1, pSndFile->m_MixPlugins[m_pVstPlugin->GetSlot()].GetName()); - strncpy(pIns->filename, pSndFile->m_MixPlugins[m_pVstPlugin->GetSlot()].GetLibraryName(), CountOf(pIns->filename) - 1); + StringFixer::CopyN(pIns->filename, pSndFile->m_MixPlugins[m_pVstPlugin->GetSlot()].GetLibraryName()); pIns->nMixPlug = (PLUGINDEX)m_pVstPlugin->GetSlot() + 1; pIns->nMidiChannel = 1; // People will forget to change this anyway, so the following lines can lead to some bad surprises after re-opening the module. Modified: trunk/OpenMPT/mptrack/CleanupSong.cpp =================================================================== --- trunk/OpenMPT/mptrack/CleanupSong.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/CleanupSong.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -328,7 +328,7 @@ { //strncpy_s(pTTTA->szText, sizeof(pTTTA->szText), strTipText, // strTipText.GetLength() + 1); - strncpy(pTTTA->szText, strTipText, min(strTipText.GetLength() + 1, CountOf(pTTTA->szText) - 1)); + StringFixer::CopyN(pTTTA->szText, strTipText); } else { Modified: trunk/OpenMPT/mptrack/Ctrl_ins.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_ins.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Ctrl_ins.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -1269,15 +1269,10 @@ m_SliderResSwing.EnableWindow((pIns != nullptr && (m_pSndFile->GetType() == MOD_TYPE_MPT || pIns->nResSwing != 0)) ? TRUE : FALSE); m_CbnFilterMode.EnableWindow((pIns != nullptr && (m_pSndFile->GetType() == MOD_TYPE_MPT || pIns->nFilterMode != FLTMODE_UNCHANGED)) ? TRUE : FALSE); - CHAR s[128]; if (pIns) { - memcpy(s, pIns->name, 32); - s[32] = 0; - m_EditName.SetWindowText(s); - memcpy(s, pIns->filename, 12); - s[12] = 0; - m_EditFileName.SetWindowText(s); + m_EditName.SetWindowText(pIns->name); + m_EditFileName.SetWindowText(pIns->filename); // Fade Out Volume SetDlgItemInt(IDC_EDIT7, pIns->nFadeOut); // Global Volume @@ -1491,16 +1486,12 @@ if (!pIns->name[0] && m_pSndFile->GetModSpecifications().instrNameLengthMax > 0) { - strncpy(pIns->name, szName, CountOf(pIns->name) - 1); - ASSERT(m_pSndFile->GetModSpecifications().instrNameLengthMax < CountOf(pIns->name)); - pIns->name[m_pSndFile->GetModSpecifications().instrNameLengthMax] = '\0'; + StringFixer::CopyN(pIns->name, szName, m_pSndFile->GetModSpecifications().instrNameLengthMax); } if (!pIns->filename[0] && m_pSndFile->GetModSpecifications().instrFilenameLengthMax > 0) { strcat(szName, szExt); - strncpy(pIns->filename, szName, CountOf(pIns->filename) - 1); - ASSERT(m_pSndFile->GetModSpecifications().instrFilenameLengthMax < CountOf(pIns->filename)); - pIns->filename[m_pSndFile->GetModSpecifications().instrFilenameLengthMax] = '\0'; + StringFixer::CopyN(pIns->filename, szName, m_pSndFile->GetModSpecifications().instrFilenameLengthMax); } SetCurrentInstrument(m_nInstrument); @@ -1803,12 +1794,11 @@ if (!pIns) return; if (pIns->filename[0]) { - strncpy(szFileName, pIns->filename, min(CountOf(pIns->filename), CountOf(szFileName) - 1)); + StringFixer::Copy(szFileName, pIns->filename); } else { - strncpy(szFileName, pIns->name, min(CountOf(pIns->name), CountOf(szFileName) - 1)); + StringFixer::Copy(szFileName, pIns->name); } - StringFixer::SetNullTerminator(szFileName); SanitizeFilename(szFileName); int index = 0; @@ -1881,11 +1871,10 @@ CHAR s[64]; s[0] = 0; m_EditName.GetWindowText(s, sizeof(s)); - for (UINT i=strlen(s); i<=32; i++) s[i] = 0; ModInstrument *pIns = m_pSndFile->Instruments[m_nInstrument]; - if ((pIns) && (strncmp(s, pIns->name, 32))) + if ((pIns) && (strncmp(s, pIns->name, MAX_INSTRUMENTNAME))) { - memcpy(pIns->name, s, 32); + StringFixer::Copy(pIns->name, s); SetInstrumentModified(true); } } Modified: trunk/OpenMPT/mptrack/Ctrl_pat.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_pat.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Ctrl_pat.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -1117,13 +1117,13 @@ m_EditPatName.GetWindowText(s, CountOf(s)); StringFixer::SetNullTerminator(s); - if (m_pSndFile->Patterns[nPat].GetName().Compare(s)) + if(m_pSndFile->Patterns[nPat].GetName() != s) { - if(m_pSndFile->Patterns[nPat].SetName(s)) - { - if (m_pSndFile->m_nType & (MOD_TYPE_XM|MOD_TYPE_IT|MOD_TYPE_MPT)) m_pModDoc->SetModified(); - m_pModDoc->UpdateAllViews(NULL, (nPat << HINT_SHIFT_PAT) | HINT_PATNAMES, this); - } + if(m_pSndFile->Patterns[nPat].SetName(s)) + { + if(m_pSndFile->GetType() & (MOD_TYPE_XM|MOD_TYPE_IT|MOD_TYPE_MPT)) m_pModDoc->SetModified(); + m_pModDoc->UpdateAllViews(NULL, (nPat << HINT_SHIFT_PAT) | HINT_PATNAMES, this); + } } } } Modified: trunk/OpenMPT/mptrack/Ctrl_seq.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_seq.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Ctrl_seq.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -1014,7 +1014,7 @@ for(SEQUENCEINDEX i = 0; i < numSequences; i++) { CString str; - if(sndFile.Order.GetSequence(i).m_sName.IsEmpty()) + if(sndFile.Order.GetSequence(i).m_sName.empty()) str.Format(TEXT("Sequence %u"), i); else str.Format(TEXT("%u: %s"), i, (LPCTSTR)sndFile.Order.GetSequence(i).m_sName); Modified: trunk/OpenMPT/mptrack/Ctrl_smp.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -668,12 +668,10 @@ wsprintf(s, "%d-bit %s, len: %d", sample.GetElementarySampleSize() * 8, (sample.uFlags & CHN_STEREO) ? "stereo" : "mono", sample.nLength); SetDlgItemText(IDC_TEXT5, s); // Name - memcpy(s, m_pSndFile->m_szNames[m_nSample], MAX_SAMPLENAME); - s[31] = 0; + StringFixer::Copy(s, m_pSndFile->m_szNames[m_nSample]); SetDlgItemText(IDC_SAMPLE_NAME, s); // File Name - memcpy(s, sample.filename, MAX_SAMPLEFILENAME); - s[21] = 0; + StringFixer::Copy(s, sample.filename); if (m_pSndFile->GetType() & (MOD_TYPE_MOD | MOD_TYPE_XM)) s[0] = 0; SetDlgItemText(IDC_SAMPLE_FILENAME, s); // Volume @@ -848,11 +846,10 @@ { // S3M/IT szFullFilename[31] = 0; - if (!m_pSndFile->m_szNames[m_nSample][0]) memcpy(m_pSndFile->m_szNames[m_nSample], szFullFilename, MAX_SAMPLENAME); + if (!m_pSndFile->m_szNames[m_nSample][0]) StringFixer::Copy(m_pSndFile->m_szNames[m_nSample], szFullFilename); if (strlen(szFullFilename) < 9) strcat(szFullFilename, szExt); } - szFullFilename[21] = 0; - memcpy(sample.filename, szFullFilename, MAX_SAMPLEFILENAME); + StringFixer::Copy(sample.filename, szFullFilename); } if ((m_pSndFile->GetType() & MOD_TYPE_XM) && (!(sample.uFlags & CHN_PANNING))) { @@ -1029,11 +1026,11 @@ } if(m_pSndFile->GetType() & (MOD_TYPE_S3M|MOD_TYPE_IT|MOD_TYPE_MPT)) { - strncpy(szFileName, m_pSndFile->GetSample(m_nSample).filename, Util::Min(CountOf(m_pSndFile->GetSample(m_nSample).filename), CountOf(szFileName) - 1)); + StringFixer::Copy(szFileName, m_pSndFile->GetSample(m_nSample).filename); } if(!szFileName[0]) { - strncpy(szFileName, m_pSndFile->m_szNames[m_nSample], Util::Min(CountOf(m_pSndFile->m_szNames[m_nSample]), CountOf(szFileName) - 1)); + StringFixer::Copy(szFileName, m_pSndFile->m_szNames[m_nSample]); } if(!szFileName[0]) strcpy(szFileName, "untitled"); if(strlen(szFileName) >= 5 && !_strcmpi(szFileName + strlen(szFileName) - 5, ".flac")) @@ -2364,7 +2361,7 @@ s[31] = 0; if (strncmp(s, m_pSndFile->m_szNames[m_nSample], MAX_SAMPLENAME)) { - memcpy(m_pSndFile->m_szNames[m_nSample], s, MAX_SAMPLENAME); + StringFixer::Copy(m_pSndFile->m_szNames[m_nSample], s); m_pModDoc->UpdateAllViews(NULL, (m_nSample << HINT_SHIFT_SMP) | (HINT_SMPNAMES|HINT_SAMPLEINFO), this); m_pModDoc->UpdateAllViews(NULL, HINT_INSNAMES, this); m_pModDoc->SetModified(); @@ -2384,7 +2381,7 @@ if (strncmp(s, m_pSndFile->GetSample(m_nSample).filename, MAX_SAMPLEFILENAME)) { - memcpy(m_pSndFile->GetSample(m_nSample).filename, s, MAX_SAMPLEFILENAME); + StringFixer::Copy(m_pSndFile->GetSample(m_nSample).filename, s); m_pModDoc->UpdateAllViews(NULL, (m_nSample << HINT_SHIFT_SMP) | HINT_SAMPLEINFO, this); if (m_pSndFile->m_nType & (MOD_TYPE_S3M|MOD_TYPE_IT|MOD_TYPE_MPT)) m_pModDoc->SetModified(); } Modified: trunk/OpenMPT/mptrack/Draw_pat.cpp =================================================================== --- trunk/OpenMPT/mptrack/Draw_pat.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Draw_pat.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -1599,7 +1599,7 @@ // display plugin name. if(m->instr <= MAX_MIXPLUGINS) { - strncpy(sztmp, pSndFile->m_MixPlugins[m->instr - 1].GetName(), CountOf(sztmp)); + StringFixer::CopyN(sztmp, pSndFile->m_MixPlugins[m->instr - 1].GetName()); } } else { @@ -1609,8 +1609,7 @@ if ((m->instr <= pSndFile->GetNumInstruments()) && (pSndFile->Instruments[m->instr])) { ModInstrument *pIns = pSndFile->Instruments[m->instr]; - memcpy(sztmp, pIns->name, 32); - sztmp[32] = 0; + StringFixer::Copy(sztmp, pIns->name); if ((m->note) && (m->note <= NOTE_MAX)) { const SAMPLEINDEX nsmp = pIns->Keyboard[m->note - 1]; @@ -1627,8 +1626,7 @@ { if (m->instr <= pSndFile->GetNumSamples()) { - memcpy(sztmp, pSndFile->m_szNames[m->instr], MAX_SAMPLENAME); - sztmp[32] = 0; + StringFixer::Copy(sztmp, pSndFile->m_szNames[m->instr]); } } @@ -1646,8 +1644,7 @@ if(m->instr > 0 && m->instr <= MAX_MIXPLUGINS) { CHAR sztmp[128] = ""; - strncpy(sztmp, pSndFile->m_MixPlugins[m->instr - 1].GetParamName(m->GetValueVolCol()), sizeof(sztmp)); - StringFixer::SetNullTerminator(sztmp); + StringFixer::CopyN(sztmp, pSndFile->m_MixPlugins[m->instr - 1].GetParamName(m->GetValueVolCol())); if (sztmp[0]) wsprintf(s, "%d: %s", m->GetValueVolCol(), sztmp); } } else Modified: trunk/OpenMPT/mptrack/Moddoc.cpp =================================================================== --- trunk/OpenMPT/mptrack/Moddoc.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Moddoc.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -276,7 +276,7 @@ pIns = m_SndFile.Instruments[nIns]; // Reset pIns because ExtractInstrument may delete the previous value. if ((dwKey >= 24) && (dwKey < 100)) { - lstrcpyn(pIns->name, szMidiPercussionNames[dwKey-24], sizeof(pIns->name)); + StringFixer::CopyN(pIns->name, szMidiPercussionNames[dwKey - 24]); } bEmbedded = TRUE; } @@ -318,7 +318,7 @@ pIns = m_SndFile.Instruments[nIns]; // Reset pIns because ExtractInstrument may delete the previous value. if ((dwKey >= 24) && (dwKey < 24+61)) { - lstrcpyn(pIns->name, szMidiPercussionNames[dwKey-24], sizeof(pIns->name)); + StringFixer::CopyN(pIns->name, szMidiPercussionNames[dwKey-24]); } } } @@ -339,17 +339,17 @@ _splitpath(pszMidiMapName, NULL, NULL, szName, szExt); strncat(szName, szExt, sizeof(szName)); pIns = m_SndFile.Instruments[nIns]; - if (!pIns->filename[0]) lstrcpyn(pIns->filename, szName, sizeof(pIns->filename)); + if (!pIns->filename[0]) StringFixer::Copy(pIns->filename, szName); if (!pIns->name[0]) { if (nMidiCode < 128) { - lstrcpyn(pIns->name, szMidiProgramNames[nMidiCode], sizeof(pIns->name)); + StringFixer::CopyN(pIns->name, szMidiProgramNames[nMidiCode]); } else { UINT nKey = nMidiCode & 0x7F; if (nKey >= 24) - lstrcpyn(pIns->name, szMidiPercussionNames[nKey-24], sizeof(pIns->name)); + StringFixer::CopyN(pIns->name, szMidiPercussionNames[nKey - 24]); } } } @@ -521,7 +521,7 @@ if(instr > 0 && instr <= GetNumInstruments()) { instr--; - if(!m_SndFile.m_szInstrumentPath[instr].IsEmpty()) + if(!m_SndFile.m_szInstrumentPath[instr].empty()) { const size_t len = m_SndFile.m_szInstrumentPath[instr].length(); const bool iti = _stricmp(&m_SndFile.m_szInstrumentPath[instr][len - 3], "iti") == 0; Modified: trunk/OpenMPT/mptrack/Modedit.cpp =================================================================== --- trunk/OpenMPT/mptrack/Modedit.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Modedit.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -528,7 +528,7 @@ } InitializeInstrument(instrument); - lstrcpyn(instrument->name, m_SndFile.m_szNames[smp], MAX_INSTRUMENTNAME); + StringFixer::Copy(instrument->name, m_SndFile.m_szNames[smp]); MuteInstrument(smp, muted); } Modified: trunk/OpenMPT/mptrack/Mptrack.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Mptrack.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -157,7 +157,7 @@ CHAR s[_MAX_PATH], *pszCmd, *pszData; int len; - lstrcpyn(s, lpszCommand, CountOf(s)); + StringFixer::CopyN(s, lpszCommand); len = strlen(s) - 1; while ((len > 0) && (strchr("(){}[]\'\" ", s[len]))) s[len--] = 0; pszCmd = s; Modified: trunk/OpenMPT/mptrack/PatternEditorDialogs.cpp =================================================================== --- trunk/OpenMPT/mptrack/PatternEditorDialogs.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/PatternEditorDialogs.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -920,7 +920,7 @@ if (pSndFile->GetNumInstruments()) { if (pSndFile->Instruments[i]) - memcpy(s+k, pSndFile->Instruments[i]->name, 32); + memcpy(s + k, pSndFile->Instruments[i]->name, CountOf(pSndFile->Instruments[i]->name)); } else memcpy(s+k, pSndFile->m_szNames[i], MAX_SAMPLENAME); s[k+32] = 0; Modified: trunk/OpenMPT/mptrack/SelectPluginDialog.cpp =================================================================== --- trunk/OpenMPT/mptrack/SelectPluginDialog.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/SelectPluginDialog.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -152,10 +152,8 @@ break; } - lstrcpyn(m_pPlugin->Info.szName, pFactory->szLibraryName, 32); - lstrcpyn(m_pPlugin->Info.szLibraryName, pFactory->szLibraryName, 64); - StringFixer::SetNullTerminator(m_pPlugin->Info.szName); - StringFixer::SetNullTerminator(m_pPlugin->Info.szLibraryName); + StringFixer::Copy(m_pPlugin->Info.szName, pFactory->szLibraryName); + StringFixer::Copy(m_pPlugin->Info.szLibraryName, pFactory->szLibraryName); cs.Leave(); @@ -171,7 +169,7 @@ if ((p->GetDefaultEffectName(s)) && (s[0])) { s[31] = 0; - lstrcpyn(m_pPlugin->Info.szName, s, 32); + StringFixer::Copy(m_pPlugin->Info.szName, s); } } } Modified: trunk/OpenMPT/mptrack/View_gen.cpp =================================================================== --- trunk/OpenMPT/mptrack/View_gen.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/View_gen.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -877,7 +877,7 @@ StringFixer::SetNullTerminator(s); if (strcmp(s, pSndFile->m_MixPlugins[m_nCurrentPlugin].GetName())) { - lstrcpyn(pSndFile->m_MixPlugins[m_nCurrentPlugin].Info.szName, s, 32); + StringFixer::Copy(pSndFile->m_MixPlugins[m_nCurrentPlugin].Info.szName, s); if(pSndFile->GetModSpecifications().supportsPlugins) pModDoc->SetModified(); pModDoc->UpdateAllViews(NULL, HINT_MODCHANNELS | (m_nActiveTab << HINT_SHIFT_CHNTAB)); Modified: trunk/OpenMPT/mptrack/View_tre.cpp =================================================================== --- trunk/OpenMPT/mptrack/View_tre.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/View_tre.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -801,7 +801,7 @@ { // more than one sequence -> add folder CString sSeqName; - if(sndFile.Order.GetSequence(nSeq).m_sName.IsEmpty()) + if(sndFile.Order.GetSequence(nSeq).m_sName.empty()) sSeqName.Format("Sequence %d", nSeq); else sSeqName.Format("%d: %s", nSeq, (LPCTSTR)sndFile.Order.GetSequence(nSeq).m_sName); @@ -1008,7 +1008,7 @@ if(sndFile.m_SongFlags[SONG_ITPROJECT]) { // path info for ITP instruments - const bool pathOk = !sndFile.m_szInstrumentPath[nIns - 1].IsEmpty(); + const bool pathOk = !sndFile.m_szInstrumentPath[nIns - 1].empty(); const bool instMod = pDoc->m_bsInstrumentModified.test(nIns - 1); wsprintf(s, pathOk ? (instMod ? "%3d: * %s" : "%3d: %s") : "%3d: ? %s", nIns, (LPCTSTR)sndFile.GetInstrumentName(nIns)); } else @@ -1322,7 +1322,7 @@ { // Preview sample / instrument in module char szName[16]; - lstrcpyn(szName, GetItemText(hItem), sizeof(szName)); + StringFixer::CopyN(szName, GetItemText(hItem)); const size_t n = ConvertStrTo<size_t>(szName); CMainFrame *pMainFrm = CMainFrame::GetMainFrame(); if (pMainFrm && m_SongFile) @@ -1619,7 +1619,7 @@ ModInstrument *pIns = m_SongFile->Instruments[iIns]; if(pIns) { - lstrcpyn(szPath, pIns->name, 32); + StringFixer::Copy(szPath, pIns->name); wsprintf(s, "%3d: %s", iIns, szPath); ModTreeBuildTVIParam(tvis, s, IMAGE_INSTRUMENTS); InsertItem(&tvis); @@ -1628,7 +1628,7 @@ for(SAMPLEINDEX iSmp = 1; iSmp <= m_SongFile->GetNumSamples(); iSmp++) { const ModSample &sample = m_SongFile->GetSample(iSmp); - lstrcpyn(szPath, m_SongFile->m_szNames[iSmp], 32); + strcpy(szPath, m_SongFile->m_szNames[iSmp]); if (sample.pSample) { wsprintf(s, "%3d: %s", iSmp, szPath); @@ -1996,7 +1996,7 @@ if (m_szSongName[0]) { CHAR s[32]; - lstrcpyn(s, GetItemText(m_hItemDrag), CountOf(s)); + StringFixer::CopyN(s, GetItemText(m_hItemDrag)); UINT n = 0; if (s[0] >= '0') n += (s[0] - '0'); if ((s[1] >= '0') && (s[1] <= '9')) n = n*10 + (s[1] - '0'); @@ -3192,7 +3192,7 @@ if(pSndFile && modItemID) { - if(pSndFile->m_szInstrumentPath[modItemID - 1].IsEmpty()) + if(pSndFile->m_szInstrumentPath[modItemID - 1].empty()) { FileDlgResult files = CTrackApp::ShowOpenSaveFileDialog(false, (pSndFile->GetType() == MOD_TYPE_XM) ? "xi" : "iti", "", (pSndFile->GetType() == MOD_TYPE_XM) ? @@ -3612,7 +3612,7 @@ case MODITEM_SAMPLE: if(modItemID <= sndFile.GetNumSamples() && strcmp(sndFile.m_szNames[modItemID], info->item.pszText)) { - strncpy(sndFile.m_szNames[modItemID], info->item.pszText, modSpecs.sampleNameLengthMax); + StringFixer::CopyN(sndFile.m_szNames[modItemID], info->item.pszText, modSpecs.sampleNameLengthMax); modDoc->SetModified(); modDoc->UpdateAllViews(NULL, (UINT(modItemID) << HINT_SHIFT_SMP) | HINT_SMPNAMES | HINT_SAMPLEDATA | HINT_SAMPLEINFO); } @@ -3621,7 +3621,7 @@ case MODITEM_INSTRUMENT: if(modItemID <= sndFile.GetNumInstruments() && sndFile.Instruments[modItemID] != nullptr && strcmp(sndFile.Instruments[modItemID]->name, info->item.pszText)) { - strncpy(sndFile.Instruments[modItemID]->name, info->item.pszText, modSpecs.instrNameLengthMax); + StringFixer::CopyN(sndFile.Instruments[modItemID]->name, info->item.pszText, modSpecs.instrNameLengthMax); modDoc->SetModified(); modDoc->UpdateAllViews(NULL, (UINT(modItemID) << HINT_SHIFT_INS) | HINT_ENVELOPE | HINT_INSTRUMENT); } Modified: trunk/OpenMPT/mptrack/Vstplug.cpp =================================================================== --- trunk/OpenMPT/mptrack/Vstplug.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Vstplug.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -49,8 +49,7 @@ //---------------------------------------------- { char fn[_MAX_PATH]; - strncpy(fn, s, sizeof(fn)); - fn[sizeof(fn)-1] = 0; + StringFixer::Copy(fn, s); int f; for(f = 0; fn[f] != 0; f++) fn[f] = toupper(fn[f]); return LittleEndian(crc32(0, (BYTE *)fn, f)); @@ -161,8 +160,7 @@ p->dwPluginId1 = kDmoMagic; p->dwPluginId2 = clsid.Data1; p->category = VSTPluginLib::catDMO; - lstrcpyn(p->szLibraryName, s, sizeof(p->szLibraryName)); - StringFixer::SetNullTerminator(p->szLibraryName); + StringFixer::Copy(p->szLibraryName, s); StringFromGUID2(clsid, w, 100); WideCharToMultiByte(CP_ACP, 0, w, -1, p->szDllPath, sizeof(p->szDllPath), nullptr, nullptr); @@ -556,10 +554,10 @@ { // Try finding the plugin DLL in the plugin directory instead. CHAR s[_MAX_PATH]; - strncpy(s, TrackerSettings::Instance().GetDefaultDirectory(DIR_PLUGINS), CountOf(s)); - if(!strcmp(s, "")) + StringFixer::CopyN(s, TrackerSettings::Instance().GetDefaultDirectory(DIR_PLUGINS)); + if(!s[0]) { - strncpy(s, theApp.GetAppDirPath(), CountOf(s)); + StringFixer::CopyN(s, theApp.GetAppDirPath()); } size_t len = strlen(s); if((len > 0) && (s[len - 1] != '\\') && (s[len - 1] != '/')) @@ -1197,7 +1195,7 @@ MemsetZero(szInitPath); if(fileSel->initialPath) { - strncpy(szInitPath, fileSel->initialPath, _MAX_PATH - 1); + StringFixer::CopyN(szInitPath, fileSel->initialPath); } char szBuffer[_MAX_PATH]; Modified: trunk/OpenMPT/mptrack/Vstplug.h =================================================================== --- trunk/OpenMPT/mptrack/Vstplug.h 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/Vstplug.h 2013-03-25 23:14:59 UTC (rev 1696) @@ -76,8 +76,7 @@ category = catUnknown; if(dllPath != nullptr) { - lstrcpyn(szDllPath, dllPath, CountOf(szDllPath)); - StringFixer::SetNullTerminator(szDllPath); + StringFixer::CopyN(szDllPath, dllPath); } } Modified: trunk/OpenMPT/mptrack/dlg_misc.cpp =================================================================== --- trunk/OpenMPT/mptrack/dlg_misc.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/dlg_misc.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -449,7 +449,7 @@ //strncpy_s(pTTTA->szText, sizeof(pTTTA->szText), strTipText, // strTipText.GetLength() + 1); // 80 chars max?! - strncpy(pTTTA->szText, strTipText, min(strTipText.GetLength() + 1, CountOf(pTTTA->szText) - 1)); + StringFixer::CopyN(pTTTA->szText, strTipText); } else { ::MultiByteToWideChar(CP_ACP , 0, strTipText, strTipText.GetLength() + 1, Modified: trunk/OpenMPT/mptrack/mod2midi.cpp =================================================================== --- trunk/OpenMPT/mptrack/mod2midi.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/mod2midi.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -404,7 +404,7 @@ for (CHANNELINDEX iInit = 0; iInit < m_pSndFile->GetNumChannels(); iInit++) { DYNMIDITRACK &track = Tracks[iInit]; - lstrcpyn(s, m_pSndFile->ChnSettings[iInit].szName, MAX_CHANNELNAME); + StringFixer::Copy(s, m_pSndFile->ChnSettings[iInit].szName); track.nMidiChannel = iInit & 7; if (s[0]) { Modified: trunk/OpenMPT/mptrack/view_com.cpp =================================================================== --- trunk/OpenMPT/mptrack/view_com.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/mptrack/view_com.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -263,7 +263,7 @@ switch(iCol) { case SMPLIST_SAMPLENAME: - lstrcpyn(s, pSndFile->m_szNames[iSmp + 1], MAX_SAMPLENAME); + StringFixer::Copy(s, pSndFile->m_szNames[iSmp + 1]); break; case SMPLIST_SAMPLENO: wsprintf(s, "%02d", iSmp + 1); @@ -278,7 +278,7 @@ } break; case SMPLIST_TYPE: - if (sample.nLength) + if(sample.nLength) { wsprintf(s, "%d Bit", sample.GetElementarySampleSize() * 8); } @@ -363,7 +363,7 @@ switch(iCol) { case INSLIST_INSTRUMENTNAME: - if (pIns) lstrcpyn(s, pIns->name, sizeof(pIns->name)); + if (pIns) StringFixer::Copy(s, pIns->name); break; case INSLIST_INSTRUMENTNO: wsprintf(s, "%02d", iIns+1); @@ -515,8 +515,7 @@ { if(iItem < pSndFile->GetNumSamples()) { - strncpy(pSndFile->m_szNames[iItem + 1], lvItem.pszText, MAX_SAMPLENAME); - StringFixer::SetNullTerminator(pSndFile->m_szNames[iItem + 1]); + StringFixer::CopyN(pSndFile->m_szNames[iItem + 1], lvItem.pszText); pModDoc->UpdateAllViews(this, ((iItem + 1) << HINT_SHIFT_SMP) | (HINT_SMPNAMES | HINT_SAMPLEINFO), this); pModDoc->SetModified(); } @@ -525,8 +524,7 @@ if((iItem < pSndFile->GetNumInstruments()) && (pSndFile->Instruments[iItem + 1])) { ModInstrument *pIns = pSndFile->Instruments[iItem + 1]; - strncpy(pIns->name, lvItem.pszText, MAX_INSTRUMENTNAME); - StringFixer::SetNullTerminator(pIns->name); + StringFixer::CopyN(pIns->name, lvItem.pszText); pModDoc->UpdateAllViews(this, ((iItem + 1) << HINT_SHIFT_INS) | (HINT_INSNAMES | HINT_INSTRUMENT), this); pModDoc->SetModified(); } Modified: trunk/OpenMPT/soundlib/Dlsbank.cpp =================================================================== --- trunk/OpenMPT/soundlib/Dlsbank.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/Dlsbank.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -872,8 +872,7 @@ SFSAMPLE *p = (SFSAMPLE *)(pchunk+1); for (UINT i=0; i<m_nSamplesEx; i++, pDlsSmp++, p++) { - memcpy(pDlsSmp->szName, p->achSampleName, 20); - StringFixer::SetNullTerminator(pDlsSmp->szName); + StringFixer::Copy(pDlsSmp->szName, p->achSampleName); pDlsSmp->dwLen = 0; pDlsSmp->dwSampleRate = p->dwSampleRate; pDlsSmp->byOriginalPitch = p->byOriginalPitch; @@ -1684,12 +1683,10 @@ } lstrcat(s, ")"); } - s[31] = 0; - strcpy(pIns->name, s); + StringFixer::Copy(pIns->name, s); } else { - memcpy(pIns->name, pDlsIns->szName, 32); - StringFixer::SetNullTerminator(pIns->name); + StringFixer::Copy(pIns->name, pDlsIns->szName); } int nTranspose = 0; if (pDlsIns->ulBank & F_INSTRUMENT_DRUMS) Modified: trunk/OpenMPT/soundlib/Load_itp.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_itp.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/Load_itp.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -201,7 +201,7 @@ for(INSTRUMENTINDEX ins = 0; ins < GetNumInstruments(); ins++) { - if(m_szInstrumentPath[ins].IsEmpty() || !f.Open(m_szInstrumentPath[ins])) continue; + if(m_szInstrumentPath[ins].empty() || !f.Open(m_szInstrumentPath[ins])) continue; size = f.GetLength(); LPBYTE lpFile = f.Lock(size); @@ -265,7 +265,7 @@ if(!m_SongFlags[SONG_ITPROJECT]) return false; UINT i,j = 0; - for(i = 0 ; i < m_nInstruments ; i++) { if(!m_szInstrumentPath[i].IsEmpty() || !Instruments[i+1]) j++; } + for(i = 0 ; i < m_nInstruments ; i++) { if(!m_szInstrumentPath[i].empty() || !Instruments[i+1]) j++; } if(m_nInstruments && j != m_nInstruments) return false; // Open file Modified: trunk/OpenMPT/soundlib/Load_umx.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_umx.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/Load_umx.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -289,7 +289,7 @@ m_nSamples++; if(static_cast<size_t>(objName) < names.size()) { - strncpy(m_szNames[GetNumSamples()], names[objName].c_str(), MAX_SAMPLENAME - 1); + StringFixer::CopyN(m_szNames[GetNumSamples()], names[objName].c_str()); } } } Modified: trunk/OpenMPT/soundlib/Message.cpp =================================================================== --- trunk/OpenMPT/soundlib/Message.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/Message.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -205,7 +205,7 @@ } const size_t len = strlen(m_lpszSongComments); - comments.Reserve(len); + comments.reserve(len); for(size_t i = 0; i < len; i++) { @@ -215,21 +215,21 @@ { case leCR: default: - comments.Append("\r"); + comments.append(1, '\r'); break; + case leCRLF: + comments.append(1, 'r'); + // Intentional fall-through case leLF: - comments.Append("\n"); + comments.append(1, '\n'); break; - case leCRLF: - comments.Append("\r\n"); - break; } } else { char c = m_lpszSongComments[i]; // Pre-process text if(pTextConverter != nullptr) pTextConverter(c); - comments.AppendChar(c); + comments.append(1, c); } } return comments; Modified: trunk/OpenMPT/soundlib/ModInstrument.h =================================================================== --- trunk/OpenMPT/soundlib/ModInstrument.h 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/ModInstrument.h 2013-03-25 23:14:59 UTC (rev 1696) @@ -89,7 +89,7 @@ uint8 NoteMap[128]; // Note mapping, e.g. C-5 => D-5. SAMPLEINDEX Keyboard[128]; // Sample mapping, e.g. C-5 => Sample 1 - char name[MAX_INSTRUMENTNAME]; // Note: not guaranteed to be null-terminated. + char name[MAX_INSTRUMENTNAME]; char filename[MAX_INSTRUMENTFILENAME]; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Modified: trunk/OpenMPT/soundlib/SampleFormats.cpp =================================================================== --- trunk/OpenMPT/soundlib/SampleFormats.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/SampleFormats.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -150,7 +150,7 @@ // -> CODE#0023 // -> DESC="IT project files (.itp)" - m_szInstrumentPath[nInstr - 1].Empty(); + m_szInstrumentPath[nInstr - 1].clear(); #ifdef MODPLUG_TRACKER if(GetpModDoc()) { @@ -776,8 +776,7 @@ Instruments[nInstr] = pIns; nSamples = plh->samples; if (nSamples > 16) nSamples = 16; - memcpy(pIns->name, pih->name, 16); - pIns->name[16] = 0; + StringFixer::Copy(pIns->name, pih->name); pIns->nFadeOut = 2048; if (GetType() & (MOD_TYPE_IT|MOD_TYPE_MPT)) { @@ -1673,6 +1672,10 @@ if(fadr && code != 'K[..') // copy field data in instrument's header memcpy(fadr, file.GetRawData(), size); // (except for keyboard mapping) + if(fadr && code == 'n[..') + StringFixer::SetNullTerminator(pIns->name); + if(fadr && code == 'fn[.') + StringFixer::SetNullTerminator(pIns->filename); file.Skip(size); if(code == 'dF..' && fadr != nullptr) // 'dF..' field requires additional processing. Modified: trunk/OpenMPT/soundlib/Sndfile.cpp =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/Sndfile.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -1292,9 +1292,7 @@ return TEXT(""); ASSERT(nInstr <= GetNumInstruments()); - mpt::String str; - str.AppendChars(Instruments[nInstr]->name); - return str; + return mpt::String(Instruments[nInstr]->name); } @@ -1721,8 +1719,7 @@ { if(strcmp(m_szNames[0], titleCandidate)) { - strncpy(m_szNames[0], titleCandidate, Util::Min(CountOf(m_szNames[0]), strSize)); - StringFixer::SetNullTerminator(m_szNames[0]); + StringFixer::CopyN(m_szNames[0], titleCandidate, strSize); return true; } return false; Modified: trunk/OpenMPT/soundlib/WAVTools.cpp =================================================================== --- trunk/OpenMPT/soundlib/WAVTools.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/WAVTools.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -129,8 +129,7 @@ if(isDLS) { // DLS sample -> sample filename - strncpy(sample.filename, sampleName, CountOf(sample.filename) - 1); - StringFixer::SetNullTerminator(sample.filename); + StringFixer::Copy(sample.filename, sampleName); } // Read software name Modified: trunk/OpenMPT/soundlib/pattern.cpp =================================================================== --- trunk/OpenMPT/soundlib/pattern.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/pattern.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -133,7 +133,7 @@ m_Rows = m_RowsPerBeat = m_RowsPerMeasure = 0; FreePattern(m_ModCommands); m_ModCommands = nullptr; - m_PatternName.Empty(); + m_PatternName.clear(); } Modified: trunk/OpenMPT/soundlib/patternContainer.cpp =================================================================== --- trunk/OpenMPT/soundlib/patternContainer.cpp 2013-03-25 21:52:32 UTC (rev 1695) +++ trunk/OpenMPT/soundlib/patternContainer.cpp 2013-03-25 23:14:59 UTC (rev 1696) @@ -190,7 +190,7 @@ } for(PATTERNINDEX nPat = Size(); nPat > 0; nPat--) { - if(!m_Patterns[nPat - 1].GetName().IsEmpty()) + if(!m_Patterns[nPat - 1].GetName().empty()) { return nPat; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |