Loading ...
Sorry, an error occurred while loading the content.

Re: Patch 6.1.315 (extra)

Expand Messages
  • Walter Briscoe
    In message of Sun, 2 Feb 2003 20:03:17 in , Bram Moolenaar writes ... We sure can! In W2K SP3, I
    Message 1 of 6 , Feb 3, 2003
    • 0 Attachment
      In message <200302021903.h12J3Hs01735@...> of Sun, 2 Feb 2003
      20:03:17 in , Bram Moolenaar <Bram@...> writes
      >
      >Walter Briscoe wrote:
      >
      >> In message <200302011749.h11HnOd15306@...> of Sat, 1 Feb 2003
      >> 18:49:24 in , Bram Moolenaar <Bram@...> writes
      >> >
      >> >Patch 6.1.315 (extra)
      >> >Problem: A very long hostname may lead to an unterminated string.
      >> >Failing
      >> > to obtain a hostname may result in garbage. (Walter Briscoe)
      >> >Solution: Add a NUL at the end of the hostname buffer.
      >> >Files: src/os_mac.c, src/os_msdos.c, src/os_unix.c, src/os_win16.c,
      >> > src/os_win32.c
      >> My original question on this was about the injection of "PC " on success
      >> in the os_win32.c version of hostname().
      >> Can I suggest the following on top of Patch 6.1.315 (extra)?
      >
      >Sorry, I forgot to include this. I'll do that now.
      >
      >> *** src/0os_win32.c Sun Feb 2 07:35:32 2003
      >> --- src/os_win32.c Sun Feb 2 07:48:02 2003
      >> ***************
      >> *** 2228,2237 ****
      >> DWORD cch = sizeof szHostName;
      >>
      >> if (GetComputerName(szHostName, &cch))
      >> ! {
      >> ! STRCPY(s, "PC ");
      >> ! STRNCPY(s + 3, szHostName, len - 3);
      >> ! }
      >> else
      >> STRNCPY(s, "PC (Win32 Vim)", len);
      >> s[len - 1] = NUL; /* make sure it's terminated */
      >> --- 2228,2234 ----
      >> DWORD cch = sizeof szHostName;
      >>
      >> if (GetComputerName(szHostName, &cch))
      >> ! STRNCPY(s, szHostName, len);
      >> else
      >> STRNCPY(s, "PC (Win32 Vim)", len);
      >> s[len - 1] = NUL; /* make sure it's terminated */
      >
      >Looking at this, I wonder if we should do anything with "cch", the
      >length that GetComputerName() returns. If not, can't we directly get
      >the host name into "s"?
      >
      We sure can!
      In W2K SP3, I abstracted the code into a one compilation unit program to
      develop the following.
      The truncated result behaviour of the two versions differ.
      GetComputerName() fails if the input buffer is not long enough. My
      computer name is "D3CY200J". mch_get_host_name(x,3) sets x to "D3" with
      the old code and "PC" with the new.
      As the only call of mch_get_host_name() passes a 256 byte buffer, this
      change of behaviour never happens in practice.
      I ported the code back to os_win32.c and confirmed hostname() continues
      to work.

      *** src\0os_win32.c Sun Feb 2 07:35:32 2003
      --- src\os_win32.c. Mon Feb 3 07:56:04 2003
      ***************
      *** 2224,2240 ****
      char_u *s,
      int len)
      {
      ! char szHostName[MAX_COMPUTERNAME_LENGTH + 1];
      ! DWORD cch = sizeof szHostName;
      !
      ! if (GetComputerName(szHostName, &cch))
      {
      - STRCPY(s, "PC ");
      - STRNCPY(s + 3, szHostName, len - 3);
      - }
      - else
      STRNCPY(s, "PC (Win32 Vim)", len);
      ! s[len - 1] = NUL; /* make sure it's terminated */
      }


      --- 2224,2234 ----
      char_u *s,
      int len)
      {
      ! if (!GetComputerName(s, &len))
      {
      STRNCPY(s, "PC (Win32 Vim)", len);
      ! s[len - 1] = NUL; /* make sure it's terminated */
      ! }
      }



      --
      Walter Briscoe
    • Bram Moolenaar
      ... Passing len to GetComputerName() violates types. It might work if DWORD == int , but let s not risk that. It probably breaks on Win64. How about
      Message 2 of 6 , Feb 3, 2003
      • 0 Attachment
        Walter Briscoe wrote:

        > >Looking at this, I wonder if we should do anything with "cch", the
        > >length that GetComputerName() returns. If not, can't we directly get
        > >the host name into "s"?
        > >
        > We sure can!
        > In W2K SP3, I abstracted the code into a one compilation unit program to
        > develop the following.
        > The truncated result behaviour of the two versions differ.
        > GetComputerName() fails if the input buffer is not long enough. My
        > computer name is "D3CY200J". mch_get_host_name(x,3) sets x to "D3" with
        > the old code and "PC" with the new.
        > As the only call of mch_get_host_name() passes a 256 byte buffer, this
        > change of behaviour never happens in practice.
        > I ported the code back to os_win32.c and confirmed hostname() continues
        > to work.

        Passing "len" to GetComputerName() violates types. It might work if
        "DWORD" == "int", but let's not risk that. It probably breaks on Win64.

        How about this then:

        void
        mch_get_host_name(
        char_u *s,
        int len)
        {
        DWORD cch = len;

        if (!GetComputerName(s, &cch))
        {
        STRNCPY(s, "PC (Win32 Vim)", len);
        s[len - 1] = NUL; /* make sure it's terminated */
        }
        }

        --
        Contrary to popular belief, Unix is user friendly.
        It just happens to be selective about who it makes friends with.
        -- Dave Parnas

        /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
        /// Creator of Vim - Vi IMproved -- http://www.Vim.org \\\
        \\\ Project leader for A-A-P -- http://www.A-A-P.org ///
        \\\ Help AIDS victims, buy at Amazon -- http://ICCF.nl/click1.html ///
      • Walter Briscoe
        In message of Mon, 3 Feb 2003 20:28:14 in , Bram Moolenaar writes ... I would be surprised if it
        Message 3 of 6 , Feb 4, 2003
        • 0 Attachment
          In message <200302031928.h13JSEG02846@...> of Mon, 3 Feb 2003
          20:28:14 in , Bram Moolenaar <Bram@...> writes
          >
          >Walter Briscoe wrote:
          >
          >> >Looking at this, I wonder if we should do anything with "cch", the
          >> >length that GetComputerName() returns. If not, can't we directly get
          >> >the host name into "s"?
          >> >
          >> We sure can!
          >> In W2K SP3, I abstracted the code into a one compilation unit program to
          >> develop the following.
          >> The truncated result behaviour of the two versions differ.
          >> GetComputerName() fails if the input buffer is not long enough. My
          >> computer name is "D3CY200J". mch_get_host_name(x,3) sets x to "D3" with
          >> the old code and "PC" with the new.
          >> As the only call of mch_get_host_name() passes a 256 byte buffer, this
          >> change of behaviour never happens in practice.
          >> I ported the code back to os_win32.c and confirmed hostname() continues
          >> to work.
          >
          >Passing "len" to GetComputerName() violates types. It might work if
          >"DWORD" == "int", but let's not risk that. It probably breaks on Win64.
          I would be surprised if it does break - given the abomination that is
          long long in C99. However, I agree that your idea is better than mine!
          If it does break, VC gives a diagnostic.
          warning C4133: 'function' : incompatible types - from 'short *' to 'long *'

          >
          >How about this then:
          >
          > void
          >mch_get_host_name(
          > char_u *s,
          > int len)
          >{
          > DWORD cch = len;
          >
          > if (!GetComputerName(s, &cch))
          > {
          > STRNCPY(s, "PC (Win32 Vim)", len);
          > s[len - 1] = NUL; /* make sure it's terminated */
          > }
          >}
          >

          I did a quick check with no problem with that code with Make_ivc.dsw.
          --
          Walter Briscoe
        Your message has been successfully submitted and would be delivered to recipients shortly.