Repository: xbmc/xbmc
File: xbmc/cores/DllLoader/exports/emu_msvcrt.cpp (C++)

Code before:
          for (int i = 0; i < EMU_MAX_ENVIRONMENT_ITEMS && free_position == NULL; i++)
          {
            if (dll__environ[i] != NULL)
            {
              // we only support overwriting the old values
              if (StringUtils::CompareNoCase(dll__environ[i], var, strlen(var)) == 0)
              {
                // free it first
                free(dll__environ[i]);
                dll__environ[i] = NULL;
                free_position = &dll__environ[i];
              }
            }
            else
            {
              free_position = &dll__environ[i];
            }
          }

          if (free_position != NULL)
          {
            // free position, copy value
            size = strlen(var) + strlen(value) + 2;
            *free_position = (char*)malloc(size); // for '=' and 0 termination
            if ((*free_position))
            {
              strncpy(*free_position, var, size);
              (*free_position)[size - 1] = '\0';
              strncat(*free_position, "=", size - strlen(*free_position));
              strncat(*free_position, value, size - strlen(*free_position));
              added = true;
            }
          }

        }

        free(value);
        std::free(var);
      }
    }

    return added ? 0 : -1;
  }

  char* dll_getenv(const char* szKey)
  {
    char* value = NULL;

    {
      std::unique_lock lock(dll_cs_environ);


Review comment (suggestion):
Replace `strncpy` to `strncpy_s` and `strncat` to `strncat_s` is also an option:

```c++
              strncpy_s(*free_position, size, var, size);
              (*free_position)[size - 1] = '\0';
              strncat_s(*free_position, size, "=", size - strlen(*free_position));
              strncat_s(*free_position, size, value, size - strlen(*free_position));
```

Code after:
          for (int i = 0; i < EMU_MAX_ENVIRONMENT_ITEMS && free_position == NULL; i++)
          {
            if (dll__environ[i] != NULL)
            {
              // we only support overwriting the old values
              if (StringUtils::CompareNoCase(dll__environ[i], var, strlen(var)) == 0)
              {
                // free it first
                free(dll__environ[i]);
                dll__environ[i] = NULL;
                free_position = &dll__environ[i];
              }
            }
            else
            {
              free_position = &dll__environ[i];
            }
          }

          if (free_position != NULL)
          {
            size = strlen(var) + strlen(value) + 2;
            *free_position = static_cast<char*>(malloc(size));
            if (*free_position)
            {
              snprintf(*free_position, size, "%s=%s", var, value);
              added = true;
            }
          }
        }

        free(value);
        std::free(var);
      }
    }

    return added ? 0 : -1;
  }

  char* dll_getenv(const char* szKey)
  {
    char* value = NULL;

    {
      std::unique_lock lock(dll_cs_environ);

      update_emu_environ();//apply any changes

      for (int i = 0; i < EMU_MAX_ENVIRONMENT_ITEMS && value == NULL; i++)
      {
        if (dll__environ[i])