Skip to content

Commit

Permalink
only search for versioned binary in folder of wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Aug 23, 2024
1 parent 8a577c3 commit d2cad41
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions recipe/win_forwarder.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,29 @@ int _tmain( int argc, TCHAR *argv[] )
// reconstruct commandline call we received, pointing to versioned binary;
// MAX_PATH == 32767 == maximum length for lpCommandLine argument
char forwarded[MAX_PATH];
// the first element is the name of the calling function, which we extend
strcat(forwarded, argv[0]);
// initialize buffer with dummy so that strlen below is not UB
strcat(forwarded, "xxx");

// argv[0] is the name of the calling function, which might be a relative path;
// for security reasons, get the absolute path of the binary we're calling from
// with help from the MIT-licensed https://github.com/gpakosz/whereami
void* addr = _ReturnAddress();
HMODULE hm = NULL;
// non-zero return means success
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR) addr, &hm)) {
GetModuleFileNameA(hm, forwarded, sizeof(forwarded));
}

// chop off ".exe" (if the binary we're in was invoked like that)
unsigned int len = strlen(argv[0]);
if (len > 4 && strcmp(&forwarded[len-4], ".exe") == 0)
unsigned int len = strlen(forwarded);
if (len < 4) {
// realistically, minimum len(r"C:\a") == 4; dummy value has length 3
printf( "Could not determine location of calling binary!\n" );
return GetLastError();
}
if (strcmp(&forwarded[len-4], ".exe") == 0)
// set null-terminator to finish the string early
forwarded[len-4] = '\0';
strcat(forwarded, "-{{ majorversion }}.exe");
Expand Down

0 comments on commit d2cad41

Please sign in to comment.