Win10SysProgBookSamples icon indicating copy to clipboard operation
Win10SysProgBookSamples copied to clipboard

Process Environment Variables (Modificated Version) in Chapter03

Open shalala66 opened this issue 1 year ago • 0 comments

  1. Added "if statement" by reason of Warning C28183 - https://learn.microsoft.com/en-us/cpp/code-quality/c28183?view=msvc 170&f1url=%3FappId%3DDev16IDEF1%26l%3DRU-RU%26k%3Dk(C28183)%26rd%3Dtrue

  2. Added 2nd condition to the statement due to Warning C6387. The warning generates because of "passing a potentially null value to a parameter that is marked with In annotation generates this warning" as mentioned in official documentation - https://learn.microsoft.com/en-us/cpp/code-quality/c6387?view=msvc-170&f1url=%3FappId%3DDev16IDEF1%26l%3DRU-RU%26k%3Dk(C6387)%26rd%3Dtrue

corecrt_wstring,h:

//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// Wide-Character <string.h> Functions
//
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#if defined _DEBUG && defined _CRTDBG_MAP_ALLOC
    #pragma push_macro("_wcsdup")
    #undef _wcsdup
#endif

_Check_return_
_ACRTIMP _CRTALLOCATOR wchar_t* __cdecl _wcsdup(
    _In_z_ wchar_t const* _String
    );

#if defined _DEBUG && defined _CRTDBG_MAP_ALLOC
    #pragma pop_macro("_wcsdup")
#endif



__DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(
    errno_t, wcscat_s,
           wchar_t,        _Destination,
    _In_z_ wchar_t const*, _Source
    )

corecrt_wstring.h:

//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// Secure Alternatives
//
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#if __STDC_WANT_SECURE_LIB__

    _Check_return_wat_
    _ACRTIMP errno_t __cdecl wcscat_s(
        _Inout_updates_z_(_SizeInWords) wchar_t* _Destination,
        _In_ rsize_t _SizeInWords,
        _In_z_ wchar_t const* _Source
        );
  1. There are would be occurred compiler warnings as "Warning C6262" if the stack frame size limit is appointed for "user mode" is exceeded - 16 KB = 16000 Bytes, 16 KiB = 16384 Bytes, but there are 16420 Bytes in main function. A large stack frame can cause a stack overflow. This can be solved by transferring some data (a group of data) located on the "stack" to one of the dynamic segments in either the "heap" or the "global space" or by encreased the stack size "/analyze:stacksize 32768". When "(w)char" array is "initialized" in C++, '\0' is always appended to the end, either by "manual" or "compiler". Therefore, there must be a 1 byte reservation for '\0' at the end of the array. This should be taken into when the array is "initialized".

  2. Changed the "WCHAR text[8192] = { 0 };" to "WCHAR text[8192] = { '\0' };" through the definitions in related headers such as winnt.h, wtypes.h, WTypesBase.h. For example one of them:

winnnt.h:

//
// UNICODE (Wide Character) types
//

#ifndef _MAC
typedef wchar_t WCHAR;    // wc,   16-bit UNICODE character

Eventually the changed code snippet:

// ProcessEnvironmentVariablesModificated.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <windows.h>

WCHAR text[8192] = { '\0' };  // C6262

int main(int argc, char* argv[])
{
	PWSTR env = ::GetEnvironmentStrings();
	auto p = env;
	if (p) {  
		while (*p) {   // C28183
			auto equals = wcschr(p, L'=');

			if (equals != p && equals > 0) {  // C6387
				wcsncat_s(text, p, equals - p);
				wcscat_s(text, L": ");
				wcscat_s(text, equals + 1);   // C6387
				wcscat_s(text, L"\n");
			}
			p += wcslen(p) + 1;   // C6387
		}

		::FreeEnvironmentStrings(env);   // C28183
	}

	printf("%ls", text);

	return 0;
}

Note: This commit - "Process Environment Variables (Modificated Version)" - has been PR to original repo but still has not been merged.

shalala66 avatar Nov 13 '23 17:11 shalala66