Skip to content

Commit

Permalink
Fix WcaErrorMessage cArgs==-1 case.
Browse files Browse the repository at this point in the history
- Count args before creating message record.
- Document terminating NULL requirement.
- Add terminating NULL in MessageExit* macros.
- Enhance tests for problems encountered fixing this nightmare.

Fixes wixtoolset/issues#7422.
Fixes wixtoolset/issues#7444.
  • Loading branch information
barnson authored and robmen committed Jun 3, 2023
1 parent d5cca4a commit fe57e71
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/libs/wcautil/WixToolset.WcaUtil/inc/wcautil.h
Expand Up @@ -15,9 +15,9 @@ extern "C" {

#include "dutil.h"

#define MessageExitOnLastErrorSource(d, x, e, s, ...) { x = ::GetLastError(); x = HRESULT_FROM_WIN32(x); if (FAILED(x)) { ExitTraceSource(d, x, s, __VA_ARGS__); WcaErrorMessage(e, x, MB_OK, -1, __VA_ARGS__); goto LExit; } }
#define MessageExitOnFailureSource(d, x, e, s, ...) if (FAILED(x)) { ExitTraceSource(d, x, s, __VA_ARGS__); WcaErrorMessage(e, x, INSTALLMESSAGE_ERROR | MB_OK, -1, __VA_ARGS__); goto LExit; }
#define MessageExitOnNullWithLastErrorSource(d, p, x, e, s, ...) if (NULL == p) { x = ::GetLastError(); x = HRESULT_FROM_WIN32(x); if (!FAILED(x)) { x = E_FAIL; } ExitTraceSource(d, x, s, __VA_ARGS__); WcaErrorMessage(e, x, MB_OK, -1, __VA_ARGS__); goto LExit; }
#define MessageExitOnLastErrorSource(d, x, e, s, ...) { x = ::GetLastError(); x = HRESULT_FROM_WIN32(x); if (FAILED(x)) { ExitTraceSource(d, x, s, __VA_ARGS__); WcaErrorMessage(e, x, MB_OK, -1, __VA_ARGS__, NULL); goto LExit; } }
#define MessageExitOnFailureSource(d, x, e, s, ...) if (FAILED(x)) { ExitTraceSource(d, x, s, __VA_ARGS__); WcaErrorMessage(e, x, INSTALLMESSAGE_ERROR | MB_OK, -1, __VA_ARGS__, NULL); goto LExit; }
#define MessageExitOnNullWithLastErrorSource(d, p, x, e, s, ...) if (NULL == p) { x = ::GetLastError(); x = HRESULT_FROM_WIN32(x); if (!FAILED(x)) { x = E_FAIL; } ExitTraceSource(d, x, s, __VA_ARGS__); WcaErrorMessage(e, x, MB_OK, -1, __VA_ARGS__, NULL); goto LExit; }

#define MessageExitOnLastError(x, e, s, ...) MessageExitOnLastErrorSource(DUTIL_SOURCE_DEFAULT, x, e, s, __VA_ARGS__)
#define MessageExitOnFailure(x, e, s, ...) MessageExitOnFailureSource(DUTIL_SOURCE_DEFAULT, x, e, s, __VA_ARGS__)
Expand Down
33 changes: 20 additions & 13 deletions src/libs/wcautil/WixToolset.WcaUtil/wcawrap.cpp
Expand Up @@ -27,7 +27,9 @@ WcaErrorMessage() - sends an error message from the CustomAction using
the Error table
NOTE: Any and all var_args (...) must be WCHAR*
If you pass -1 to cArgs the count will be determined
If you pass -1 to cArgs, the count will be determined by
looking for a trailing NULL argment. If you omit a terminating
NULL, the results are undefined and probably crashy.
********************************************************************/
extern "C" UINT __cdecl WcaErrorMessage(
__in int iError,
Expand All @@ -41,6 +43,22 @@ extern "C" UINT __cdecl WcaErrorMessage(
MSIHANDLE hRec = NULL;
va_list args = NULL;

if (-1 == cArgs)
{
LPCWSTR wzArg = NULL;
va_list iter = NULL;

va_start(iter, cArgs);
cArgs = 0;

while (NULL != (wzArg = va_arg(iter, WCHAR*)) && L'\0' != *wzArg)
{
++cArgs;
}

va_end(iter);
}

uiType |= INSTALLMESSAGE_ERROR; // ensure error type is set
hRec = ::MsiCreateRecord(cArgs + 2);
if (!hRec)
Expand All @@ -56,18 +74,6 @@ extern "C" UINT __cdecl WcaErrorMessage(
ExitOnFailure(HRESULT_FROM_WIN32(er), "failed to set hresult code into error message");

va_start(args, cArgs);
if (-1 == cArgs)
{
LPCWSTR wzArg = NULL;
va_list iter = args;
cArgs = 0;

while (NULL != (wzArg = va_arg(iter, WCHAR*)) && L'\0' != *wzArg)
{
++cArgs;
}
}

for (INT i = 0; i < cArgs; i++)
{
er = ::MsiRecordSetStringW(hRec, i + 3, va_arg(args, WCHAR*));
Expand All @@ -76,6 +82,7 @@ extern "C" UINT __cdecl WcaErrorMessage(
va_end(args);

er = WcaProcessMessage(static_cast<INSTALLMESSAGE>(uiType), hRec);

LExit:
if (args)
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/burn/WixTestTools/MSIExec.cs
Expand Up @@ -111,7 +111,7 @@ public void SetDefaultArguments()
this.ForceRestart = false;
this.PromptRestart = false;
this.LogFile = String.Empty;
this.LoggingOptions = MSIExecLoggingOptions.VOICEWARMUP;
this.LoggingOptions = MSIExecLoggingOptions.Log_All_Information | MSIExecLoggingOptions.Verbose_Output | MSIExecLoggingOptions.Extra_Debugging_Information; // `/l*vx`
this.OtherArguments = String.Empty;
}

Expand Down
Expand Up @@ -10,12 +10,14 @@

<Fragment>
<util:Group Id="ShouldNotExist" Name="Should Not Exist" />
<util:Group Id="AlsoShouldNotExist" Name="Also Should Not Exist" />

<Component Id="Component1" Guid="00030829-0000-0000-C000-000000000046" Directory="INSTALLFOLDER">
<File Source="$(sys.SOURCEFILEPATH)" KeyPath="yes" />

<util:User Id="CurrentUser" Name="[LogonUser]" Domain="[%USERDOMAIN]" RemoveOnUninstall="no" Vital="no">
<util:GroupRef Id="ShouldNotExist" />
<util:GroupRef Id="AlsoShouldNotExist" />
</util:User>
</Component>
</Fragment>
Expand Down

0 comments on commit fe57e71

Please sign in to comment.