Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #163054

Re: Universal Build System for C

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Universal Build System for C
Date 2021-10-08 23:25 +0100
Organization A noiseless patient Spider
Message-ID <87o87zb1cp.fsf@bsb.me.uk> (permalink)
References (3 earlier) <4a46c648-4897-4b28-a3fc-242583460506n@googlegroups.com> <87h7ds7v8u.fsf@nosuchdomain.example.com> <18a56158-6c16-4c04-807e-74dfde2fd15an@googlegroups.com> <87wnmn5hzk.fsf@nosuchdomain.example.com> <020f64ee-7a5f-4de5-a5ed-2bb482086887n@googlegroups.com>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:

> This code can be used to self delete file on linux and windows.
> (linux remove works)

Does remove (a standard C function) not work in this situation on
Windows?

> #ifdef _WIN32
> int get_self_path(char* buffer, int maxsize)
> {
>     DWORD r = GetModuleFileNameA(NULL, buffer, maxsize);

According to online sources, GetModuleFileNameA does not always null
terminate the string on some versions of Windows.  Maybe you know you
are not running this code on old versions.

You don't check (in either version) for the result being too long for
the buffer.  This is a serious concern, since you go right ahead and
delete the resulting named file, even if it is not the one you should be
deleting!

>     return r;
> }
> #else
> int get_self_path(char* buffer, int maxsize)
> {
>     memset(buffer, 0, maxsize); // readlink does not null terminate!

There is no need to zero the buffer.  readlink returns the number of
characters written so you can use that to know where to write a null.
And if you pass maxsize and the return is maxsize you know that there
was not enough room in the buffer for the path name to be written.

>     return readlink("/proc/self/exe", buffer, maxsize);    
> }
> #endif
>
> int main()
> {
>     char szModuleName[200];
>     get_self_path(szModuleName, sizeof szModuleName);
>
> #ifdef _WIN32    
>     char szCmd[2 * MAX_PATH];
>     STARTUPINFO si = { 0 };
>     PROCESS_INFORMATION pi = { 0 };
>     StringCbPrintfA(szCmd, 2 * MAX_PATH, "cmd.exe /C timeout /t 3 && del /f /q \"%s\"", szModuleName);

For this purpose, I don't see a problem, but just in case someone
decides to copy this code for other uses, I'll point out how dangerous
it is to run commands on string that might not be properly quoted.  And,
anyway, it's an excuse to post my favourite XKCD strip:

https://xkcd.com/327/

>     CreateProcessA(NULL, szCmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
>     CloseHandle(pi.hThread);
>     CloseHandle(pi.hProcess);

That's a lot of work, so I presume just calling remove does not work.

> #else
>     remove(szModuleName);
> #endif
> }
>

-- 
Ben.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-04 05:33 -0700
  Re: Universal Build System for C Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-04 15:54 +0000
  Re: Universal Build System for C fir <profesor.fir@gmail.com> - 2021-10-04 09:26 -0700
    Re: Universal Build System for C fir <profesor.fir@gmail.com> - 2021-10-04 09:47 -0700
      Re: Universal Build System for C fir <profesor.fir@gmail.com> - 2021-10-04 10:13 -0700
        Re: Universal Build System for C fir <profesor.fir@gmail.com> - 2021-10-05 04:12 -0700
  Re: Universal Build System for C Guillaume <message@bottle.org> - 2021-10-04 19:49 +0200
    Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-04 11:00 -0700
  Re: Universal Build System for C Bart <bc@freeuk.com> - 2021-10-06 20:37 +0100
    Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-07 06:18 -0700
      Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-07 06:25 -0700
        Re: Universal Build System for C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-07 07:41 -0700
          Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-08 14:05 -0700
            Re: Universal Build System for C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-08 14:22 -0700
              Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-08 14:44 -0700
                Re: Universal Build System for C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-08 23:25 +0100
                Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-08 15:37 -0700
              Re: Universal Build System for C Bart <bc@freeuk.com> - 2021-10-08 22:51 +0100
                Re: Universal Build System for C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-10-09 01:39 -0700
                Re: Universal Build System for C Bart <bc@freeuk.com> - 2021-10-09 11:00 +0100
              Re: Universal Build System for C scott@slp53.sl.home (Scott Lurndal) - 2021-10-10 18:09 +0000
                8-bit ASCII (was Re: Universal Build System for C) scott@slp53.sl.home (Scott Lurndal) - 2021-10-10 20:55 +0000
            Re: Universal Build System for C Bart <bc@freeuk.com> - 2021-10-08 23:05 +0100
              Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-08 15:30 -0700
                Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-08 18:12 -0700
                Re: Universal Build System for C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-08 20:51 -0700
                Re: Universal Build System for C David Brown <david.brown@hesbynett.no> - 2021-10-09 12:54 +0200
                Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-09 06:30 -0700
                Re: Universal Build System for C David Brown <david.brown@hesbynett.no> - 2021-10-09 17:29 +0200
            Re: Universal Build System for C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-10-08 23:39 +0000
              Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-10-08 17:50 -0700
  Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-11-22 04:26 -0800
    Re: Universal Build System for C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-22 20:39 +0000
      Re: Universal Build System for C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-11-22 13:18 -0800
        Re: Universal Build System for C Bart <bc@freeuk.com> - 2021-11-22 21:49 +0000
          Re: Universal Build System for C scott@slp53.sl.home (Scott Lurndal) - 2021-11-23 14:45 +0000
            Re: Universal Build System for C Bart <bc@freeuk.com> - 2021-11-23 16:03 +0000
              Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2022-04-18 14:39 -0700
                Re: Universal Build System for C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-04-18 22:17 +0000
                Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2022-04-18 16:07 -0700
        Re: Universal Build System for C scott@slp53.sl.home (Scott Lurndal) - 2021-11-23 14:44 +0000
      Re: Universal Build System for C Thiago Adams <thiago.adams@gmail.com> - 2021-11-22 16:19 -0800
        Re: Universal Build System for C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-23 01:00 +0000

csiph-web