Fix GCC array bounds warning when accessing g_usrvars

git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@6258 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
anders_k 2012-09-06 22:43:31 +00:00
parent 667a2c8cae
commit c6fdb4436d
5 changed files with 18 additions and 8 deletions

View file

@ -514,14 +514,14 @@ DWORD CResourceEditor::Save(BYTE* pbBuf, DWORD &dwSize) {
// This function scans exe sections and after find a match with given name
// increments it's virtual size (auto fixes image size based on section alignment, etc)
// Jim Park: The section name must be ASCII code. Do not TCHAR this stuff.
bool CResourceEditor::AddExtraVirtualSize2PESection(const char* pszSectionName, int addsize)
bool CResourceEditor::SetPESectionVirtualSize(const char* pszSectionName, DWORD newsize)
{
PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(m_ntHeaders);
// Refresh the headers of the sections that come after the resource section, and the data directory
for (int i = 0; i < ConvertEndianness(m_ntHeaders->FileHeader.NumberOfSections); i++) {
if (!strcmp((LPCSTR)sectionHeadersArray[i].Name, pszSectionName)) {
sectionHeadersArray[i].Misc.VirtualSize = AlignVA(AdjustVA(sectionHeadersArray[i].Misc.VirtualSize, addsize));
sectionHeadersArray[i].Misc.VirtualSize = AlignVA(ConvertEndianness(newsize));
sectionHeadersArray[i].Characteristics &= ConvertEndianness((DWORD) ~IMAGE_SCN_MEM_DISCARDABLE);

View file

@ -121,7 +121,7 @@ public:
void FreeResource(BYTE* pbResource);
// The section name must be in ASCII.
bool AddExtraVirtualSize2PESection(const char* pszSectionName, int addsize);
bool SetPESectionVirtualSize(const char* pszSectionName, DWORD newsize);
DWORD Save(BYTE* pbBuf, DWORD &dwSize);
// utitlity functions

View file

@ -2321,9 +2321,8 @@ int CEXEBuild::SetVarsSection()
VerifyDeclaredUserVarRefs(&m_UserVarNames);
int MaxUserVars = m_UserVarNames.getnum();
// -1 because the default size is 1
int stringSize = NSIS_MAX_STRLEN*(build_unicode?sizeof(TCHAR):sizeof(char));
if (!res_editor->AddExtraVirtualSize2PESection(NSIS_VARS_SECTION, (MaxUserVars - 1) * stringSize))
if (!res_editor->SetPESectionVirtualSize(NSIS_VARS_SECTION, MaxUserVars * stringSize))
{
ERROR_MSG(_T("Internal compiler error #12346: invalid exehead cannot find section \"%s\"!\n"), _T(NSIS_VARS_SECTION));
return PS_ERROR;

View file

@ -18,7 +18,15 @@
#include "fileform.h"
extern NSIS_STRING g_usrvars[1];
#ifdef __GNUC__
// GCC warns about array bounds when accessing g_usrvarssection[2] because it is only [1] at compile time,
// the other part of this hack is in util.c where g_usrvarsstart is initialized.
extern const NSIS_STRING*const g_usrvarsstart;
#define g_usrvars ( (NSIS_STRING*) (g_usrvarsstart) )
#else
extern NSIS_STRING g_usrvarssection[1];
#define g_usrvars g_usrvarssection
#endif
#define state_command_line g_usrvars[20]
#define state_install_directory g_usrvars[21]

View file

@ -42,12 +42,15 @@ TCHAR g_log_file[1024];
// nsis then removes the "DISCARDABLE" style from section (for safe)
#ifdef _MSC_VER
# pragma bss_seg(NSIS_VARS_SECTION)
NSIS_STRING g_usrvars[1];
NSIS_STRING g_usrvarssection[1];
# pragma bss_seg()
# pragma comment(linker, "/section:" NSIS_VARS_SECTION ",rwd")
#else
# ifdef __GNUC__
NSIS_STRING g_usrvars[1] __attribute__((section (NSIS_VARS_SECTION)));
// GCC does not treat g_usrvarssection as a bss section so we keep the size as small as possible.
// NSIS_STRING g_usrvarssection[31] is required to remove this hack but that really bloats the exehead.
char g_usrvarssection[1] __attribute__((section (NSIS_VARS_SECTION)));
const NSIS_STRING*const g_usrvarsstart = (const NSIS_STRING*const) g_usrvarssection;
# else
# error Unknown compiler. You must implement the seperate PE section yourself.
# endif