From 1b80cdd9b469fccd169de0c8a62c779eabb0c900 Mon Sep 17 00:00:00 2001 From: Andrey Prygunkov Date: Thu, 29 Nov 2007 18:53:17 +0000 Subject: [PATCH] fixed seg fault on nzb-names starting with msgid --- DownloadInfo.cpp | 40 +++++++++++++++++++++------------------- RemoteClient.cpp | 4 +--- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/DownloadInfo.cpp b/DownloadInfo.cpp index e58ebd52..1dcffbd5 100644 --- a/DownloadInfo.cpp +++ b/DownloadInfo.cpp @@ -168,9 +168,25 @@ void FileInfo::GetNiceNZBName(char* szBuffer, int iSize) void FileInfo::MakeNiceNZBName(const char * szNZBFilename, char * szBuffer, int iSize) { - strncpy(szBuffer, BaseFileName(szNZBFilename), iSize); + char postname[1024]; + const char* szBaseName = BaseFileName(szNZBFilename); + + // if .nzb file has a certain structure, try to strip out certain elements + if (sscanf(szBaseName, "msgid_%*d_%1023s", postname) == 1) + { + // OK, using stripped name + } + else + { + // using complete filename + strncpy(postname, szBaseName, 1024); + postname[1024-1] = '\0'; + } + // wipe out ".nzb" + if (char* p = strrchr(postname, '.')) *p = '\0'; + + strncpy(szBuffer, postname, iSize); szBuffer[iSize-1] = '\0'; - if (char* p = strrchr(szBuffer, '.')) *p = '\0'; } void FileInfo::ParseSubject() @@ -222,23 +238,9 @@ void FileInfo::BuildDestDirName(const char* szNZBFilename) if (g_pOptions->GetAppendNZBDir()) { - char postname[1024]; - const char* szBaseName = BaseFileName(szNZBFilename); - - // if .nzb file has a certain structure, try to strip out certain elements - if (sscanf(szBaseName, "msgid_%*d_%1023s", postname) == 1) - { - // wipe out certain structure - memset(strrchr(postname, '.'), 0, postname + strlen(postname) - strrchr(postname, '.')); - } - else - { - strncpy(postname, szBaseName, 1024); - postname[1024-1] = '\0'; - } - // wipe out ".nzb" - memset(strrchr(postname, '.'), 0, postname + strlen(postname) - strrchr(postname, '.')); - snprintf(szBuffer, 1024, "%s%s", g_pOptions->GetDestDir(), postname); + char szNiceNZBName[1024]; + MakeNiceNZBName(szNZBFilename, szNiceNZBName, 1024); + snprintf(szBuffer, 1024, "%s%s", g_pOptions->GetDestDir(), szNiceNZBName); szBuffer[1024-1] = '\0'; } else diff --git a/RemoteClient.cpp b/RemoteClient.cpp index 5778bbb6..a574a0ea 100644 --- a/RemoteClient.cpp +++ b/RemoteClient.cpp @@ -255,9 +255,7 @@ bool RemoteClient::RequestServerList() char* szFilename = pBufPtr + sizeof(SNZBListRequestAnswerEntry) + pListAnswer->m_iNZBFilenameLen + pListAnswer->m_iSubjectLen; char szNZBNiceName[1024]; - strncpy(szNZBNiceName, BaseFileName(szNZBFilename), 1024); - szNZBNiceName[1024-1] = '\0'; - if (char* p = strrchr(szNZBNiceName, '.')) *p = '\0'; + FileInfo::MakeNiceNZBName(szNZBFilename, szNZBNiceName, 1024); printf("[%i] %s%c%s (%.2f MB%s)%s\n", pListAnswer->m_iID, szNZBNiceName, (int)PATH_SEPARATOR, szFilename, pListAnswer->m_iFileSize / 1024.0 / 1024.0, szCompleted, szStatus);