Skip to content

Bug Fix Request: Add NULL guard to write_quoted_str in civet. #2150

Description

@joshtews

If a string address is NULL when civet tries to access it the sim crashes

static void write_quoted_str( std::ostream& os, const char* s) {
int ii;
int len = strlen(s);
os << "\"" ;
for (ii=0 ; ii<len ; ii++) {
switch ((s)[ii]) {
case '\n': os << "\\n"; break;
case '\t': os << "\\t"; break;
case '\b': os << "\\b"; break;
case '\"': os << "\\\""; break;
default : os << s[ii] ; break;
}
}
os << "\"" ;
}

Crash output snippet:

  Thread 4 (MyCivetServer web thread):
  #0  __strlen_evex ()                                     from /lib64/libc.so.6
  #1  write_quoted_str (os=..., s=0x0)                     at src/VariableServerVariable.cpp:62
  #2  VariableServerVariable::writeValue (this=..., outs=...) at src/VariableServerVariable.cpp:188
  #3  VariableServerSession::sendMessage (this=...)        at src/VariableServerSession.cpp:82
  #4  main_loop (S=...)                                    at src/MyCivetServer.cpp:227

Suggested edit:

static void write_quoted_str( std::ostream& os, const char* s) {
    // Return an empty string when s is NULL
    if (!s) { 
        os << "\"\""; return; 
    }

    int ii;
    int len = strlen(s);
    os << "\"" ;
    for (ii=0 ; ii<len ; ii++) {
        switch ((s)[ii]) {
        case '\n': os << "\\n"; break;
        case '\t': os << "\\t"; break;
        case '\b': os << "\\b"; break;
        case '\"': os << "\\\""; break;
        default  : os << s[ii] ; break;
        }
    }
    os << "\"" ;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions