AppleWin/source/Registry.cpp
Brett Vickers 9e5e21b8c9 Prevent uninitialized value bugs and improve string safety.
This change does two things:

1. Updates the registry APIs to reduce the likelihood of uninitialized
variables.

The code wasn't always checking the return value of registry load operations.
In some cases, this led to uninitialized memory being used, and crashes could
result. For example, LoadConfiguration in Applewin.cpp was using an
uninitialized value for the computer type if no registry variable for the
"Apple 2 type" was set.

New registry reading methods and macros have also been introduced, allowing
default value fallbacks for the cases where a registry variable is not found.
This makes registry access simpler and safer when a default value is known in
advance.

The registry code's style has also been updated to conform with the rest of
the code base (tabs instead of spaces, naming conventions, etc.)

2. Introduces string safety improvements.

A number of code paths have been modified to use safe-string functions instead
of their unsafe counterparts (e.g., strcpy, sprintf).  In the process, some
strings were converted from "char" to "TCHAR". This was done mostly for
consistency with the rest of the code-base.
2019-08-09 13:38:50 -07:00

125 lines
3.5 KiB
C++

/*
AppleWin : An Apple //e emulator for Windows
Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2007, Tom Charlesworth, Michael Pohoreski
AppleWin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
AppleWin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Description: Registry module
*
* Author: Various
*/
#include "StdAfx.h"
//===========================================================================
BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, DWORD chars)
{
TCHAR fullkeyname[256];
StringCbPrintf(fullkeyname, 256, TEXT("Software\\AppleWin\\CurrentVersion\\%s"), section);
BOOL success = FALSE;
HKEY keyhandle;
LSTATUS status = RegOpenKeyEx(
(peruser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE),
fullkeyname,
0,
KEY_READ,
&keyhandle);
if (status == 0)
{
DWORD type;
DWORD size = chars;
status = RegQueryValueEx(keyhandle, key, NULL, &type, (LPBYTE)buffer, &size);
if (status == 0 && size != 0)
success = TRUE;
}
RegCloseKey(keyhandle);
return success;
}
//===========================================================================
BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, DWORD chars, LPCTSTR defaultValue)
{
BOOL success = RegLoadString(section, key, peruser, buffer, chars);
if (!success)
StringCbCopy(buffer, chars, defaultValue);
return success;
}
//===========================================================================
BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value) {
TCHAR buffer[32];
if (!RegLoadString(section, key, peruser, buffer, 32))
{
return FALSE;
}
*value = (DWORD)_ttoi(buffer);
return TRUE;
}
//===========================================================================
BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value, DWORD defaultValue) {
BOOL success = RegLoadValue(section, key, peruser, value);
if (!success)
*value = defaultValue;
return success;
}
//===========================================================================
void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPCTSTR buffer) {
TCHAR fullkeyname[256];
StringCbPrintf(fullkeyname, 256, TEXT("Software\\AppleWin\\CurrentVersion\\%s"), section);
HKEY keyhandle;
DWORD disposition;
LSTATUS status = RegCreateKeyEx(
(peruser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE),
fullkeyname,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_READ | KEY_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
&keyhandle,
&disposition);
if (status == 0)
{
RegSetValueEx(
keyhandle,
key,
0,
REG_SZ,
(CONST LPBYTE)buffer,
(_tcslen(buffer) + 1) * sizeof(TCHAR));
RegCloseKey(keyhandle);
}
}
//===========================================================================
void RegSaveValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD value) {
TCHAR buffer[32] = TEXT("");
StringCbPrintf(buffer, 32, "%d", value);
RegSaveString(section, key, peruser, buffer);
}