• Print

Author Topic: SHELL calls CreateProcess with an unterminated string. [fixed*]  (Read 124 times)

mcalkins

  • Hero Member
  • *****
  • Posts: 1279
    • qbasicmichael.com
    • Email
SHELL calls CreateProcess with an unterminated string. [fixed*]
« on: March 25, 2013, 12:55:42 AM »
This affects QB64 0.954 and QB64 GL 0.971b.

Code: [Select]
qbs_set(strz,qbs_new_txt("cmd.exe /c ver"));
Note that this code occurs in multiple places.

The string literal is null terminated. qbs_new_txt initializes a string descriptor, with chr pointing to the const string, readonly set to 1, and len set from strlen, which does not count the null. I believe that qbs_set then copies the string, without the null.

Why not just pass the string literal directly to CreateProcess, and not bother with the qbs?

I discovered this by using something like this, compiled as cmd.exe, to see what parameters QB64 passes to cmd:

Code: [Select]
#include <windows.h>
int main() {
 MessageBoxA(0, GetCommandLineA(), "cmd", 0);
 return 0;
}

Whether it manifests itself depends on the existing contents of the memory pointed to by strz->chr. I don't understand the details of how QB64 handles string memory enough to know how to force it to manifest itself. In the case of qb64.exe, for 0.954, it contains a list of *.bas files in the folder, and for 0.971b, it seems to contain a bunch of text related to OpenGL functions.

Regards,
Michael
« Last Edit: April 03, 2013, 09:31:20 PM by Galleon »
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4691
  • QB Forever
    • Email
Re: SHELL calls CreateProcess with an unterminated string.
« Reply #1 on: March 25, 2013, 06:46:20 AM »
Wow, that could explain a few things!
Something old... Something new... Something borrowed... Something blue...

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4691
  • QB Forever
    • Email
Re: SHELL calls CreateProcess with an unterminated string.
« Reply #2 on: April 03, 2013, 09:13:25 PM »
Amalgamated this into a function (which it should have been from the beginning) and changed the qbs to a literal string with inherit null terminator.

(didn't change SDL version, only GL)

New function as follows...
Code: [Select]
#ifdef QB64_WINDOWS
int32 cmd_available=-1;
int32 cmd_ok(){
if (cmd_available==-1){
 static STARTUPINFO si;
 static PROCESS_INFORMATION pi;
 ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) );
 if(
         CreateProcess(
          NULL,           // No module name (use command line)
          "cmd.exe /c ver",// Command line
          NULL,           // Process handle not inheritable
          NULL,           // Thread handle not inheritable
          FALSE,          // Set handle inheritance to FALSE
          CREATE_NO_WINDOW, // No creation flags
          NULL,           // Use parent's environment block
          NULL,           // Use parent's starting directory
          &si,            // Pointer to STARTUPINFO structure
          &pi             // Pointer to PROCESS_INFORMATION structure
         )
 ){
  WaitForSingleObject(pi.hProcess,INFINITE);
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  cmd_available=1;
 }else{
  cmd_available=0;
 }
}//-1
return cmd_available;
}
#endif
Something old... Something new... Something borrowed... Something blue...

  • Print