private static unsafe string? GetSysctlKernOsVersion()

in JetBrains.HabitatDetector/src/Impl/MacOsX/MacOsHelper.cs [28:49]


    private static unsafe string? GetSysctlKernOsVersion()
    {
      const int mibSize = 2;
      var mib = stackalloc int[mibSize] { SYSCTL.CTL_KERN, SYSCTL.KERN_OSVERSION };
      ulong bufLen = 0;
      if (LibSystemCDylib.sysctl(mib, mibSize, null, &bufLen, null, 0) == -1)
      {
        if (Marshal.GetLastWin32Error() == ERRNO.ENOENT)
          return null;
        throw new Exception("Failed to get size for the build number string with sysctl(CTL_KERN,KERN_OSVERSION)");
      }

      var buf = new byte[bufLen];
      fixed (byte* bufPtr = buf)
        if (LibSystemCDylib.sysctl(mib, mibSize, bufPtr, &bufLen, null, 0) == -1)
          throw new Exception("Failed to get the build number string with sysctl(CTL_KERN,KERN_OSVERSION)");

      var chars = Encoding.UTF8.GetChars(buf);
      var charsZeroIndex = Array.IndexOf(chars, '\0');
      var charsLen = charsZeroIndex < 0 ? chars.Length : charsZeroIndex;
      return new string(chars, 0, charsLen);
    }