Skip to content

Commit

Permalink
GUI: Replace use of strdup with Common::String
Browse files Browse the repository at this point in the history
strdup is a POSIX API, not an ANSI C API. It is not available with
-std=c++11 on newlib-based systems, and VS 2015 will throw errors
unless it is #defined to alias to _strdup or unless deprecation
warnings are turned off (which is not a good idea in general).
Common::String is a safer and potentially faster (due to small
string optimisation) alternative, so prefer it instead.
  • Loading branch information
csnover authored and sev- committed Aug 18, 2018
1 parent 08314ae commit b9a649c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
22 changes: 7 additions & 15 deletions gui/debugger.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ namespace GUI {
Debugger::Debugger() { Debugger::Debugger() {
_frameCountdown = 0; _frameCountdown = 0;
_isActive = false; _isActive = false;
_errStr = NULL;
_firstTime = true; _firstTime = true;
#ifndef USE_TEXT_CONSOLE_FOR_DEBUGGER #ifndef USE_TEXT_CONSOLE_FOR_DEBUGGER
_debuggerDialog = new GUI::ConsoleDialog(1.0f, 0.67f); _debuggerDialog = new GUI::ConsoleDialog(1.0f, 0.67f);
Expand Down Expand Up @@ -159,8 +158,7 @@ void Debugger::attach(const char *entry) {
g_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, true); g_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, true);


// Set error string (if any) // Set error string (if any)
free(_errStr); _errStr = entry ? entry : "";
_errStr = entry ? strdup(entry) : 0;


// Reset frame countdown (i.e. attach immediately) // Reset frame countdown (i.e. attach immediately)
_frameCountdown = 1; _frameCountdown = 1;
Expand Down Expand Up @@ -224,10 +222,9 @@ void Debugger::enter() {
_firstTime = false; _firstTime = false;
} }


if (_errStr) { if (_errStr.size()) {
debugPrintf("ERROR: %s\n\n", _errStr); debugPrintf("ERROR: %s\n\n", _errStr.c_str());
free(_errStr); _errStr.clear();
_errStr = NULL;
} }


_debuggerDialog->runModal(); _debuggerDialog->runModal();
Expand Down Expand Up @@ -293,19 +290,16 @@ bool Debugger::parseCommand(const char *inputOrig) {
const char *param[256]; const char *param[256];


// Parse out any params // Parse out any params
// One of the rare occasions using strdup is OK, since splitCommands needs to modify it Common::String input(inputOrig);
char *input = strdup(inputOrig);
splitCommand(input, num_params, &param[0]); splitCommand(input, num_params, &param[0]);


if (num_params == 0) { if (num_params == 0) {
free(input);
return true; return true;
} }


// Handle commands first // Handle commands first
bool result; bool result;
if (handleCommand(num_params, param, result)) { if (handleCommand(num_params, param, result)) {
free(input);
return result; return result;
} }


Expand Down Expand Up @@ -390,23 +384,21 @@ bool Debugger::parseCommand(const char *inputOrig) {
} }
} }


free(input);
return true; return true;
} }
} }


debugPrintf("Unknown command or variable\n"); debugPrintf("Unknown command or variable\n");
free(input);
return true; return true;
} }


void Debugger::splitCommand(char *input, int &argc, const char **argv) { void Debugger::splitCommand(Common::String &input, int &argc, const char **argv) {
byte c; byte c;
enum states { DULL, IN_WORD, IN_STRING } state = DULL; enum states { DULL, IN_WORD, IN_STRING } state = DULL;
const char *paramStart = nullptr; const char *paramStart = nullptr;


argc = 0; argc = 0;
for (char *p = input; *p; ++p) { for (Common::String::iterator p = input.begin(); *p; ++p) {
c = (byte)*p; c = (byte)*p;


switch (state) { switch (state) {
Expand Down
5 changes: 3 additions & 2 deletions gui/debugger.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "common/hashmap.h" #include "common/hashmap.h"
#include "common/hash-str.h" #include "common/hash-str.h"
#include "common/array.h" #include "common/array.h"
#include "common/str.h"
#include "common/str-array.h" #include "common/str-array.h"


namespace GUI { namespace GUI {
Expand Down Expand Up @@ -161,7 +162,7 @@ class Debugger {
*/ */
bool _isActive; bool _isActive;


char *_errStr; Common::String _errStr;


/** /**
* Initially true, set to false when Debugger::enter is called * Initially true, set to false when Debugger::enter is called
Expand Down Expand Up @@ -208,7 +209,7 @@ class Debugger {
* Splits up the input into individual parameters * Splits up the input into individual parameters
* @remarks Adapted from code provided by torek on StackOverflow * @remarks Adapted from code provided by torek on StackOverflow
*/ */
void splitCommand(char *input, int &argc, const char **argv); void splitCommand(Common::String &input, int &argc, const char **argv);


bool parseCommand(const char *input); bool parseCommand(const char *input);
bool tabComplete(const char *input, Common::String &completion) const; bool tabComplete(const char *input, Common::String &completion) const;
Expand Down

0 comments on commit b9a649c

Please sign in to comment.