|
|
|
|
|
by ploxiln
1671 days ago
|
|
That's code copied from the linked gist. But, it omits some key lines in the middle, and does not actually overflow: DWORD required_length;
auto rc = RegQueryValueExW(key, version, NULL, NULL, NULL, &required_length);
if (rc != 0) return NULL;
DWORD length = required_length + 2; // The +2 is for the maybe optional zero later on. Probably we are over-allocating.
wchar_t *value = (wchar_t *)malloc(length);
if (!value) return NULL;
rc = RegQueryValueExW(key, version, NULL, NULL, (LPBYTE)value, &length); // We know that version is zero-terminated...
if (rc != 0) return NULL;
// The documentation says that if the string for some reason was not stored
// with zero-termination, we need to manually terminate it. Sigh!!
if (value[length]) {
value[length+1] = 0;
}
`length` starts at 2 greater than naively needed, then is updated to the real value (again, always 2 less I guess). |
|
Don't poke beyond your end. Don't poke using a value that was returned by a function you don't control. The code shown does both. Such quality, it Jonathan Blow.