Skip to content

Commit 3af3d72

Browse files
committed
Do not unnecessarily use C string functions
1 parent 73e0c0f commit 3af3d72

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/goto-programs/interpreter_evaluate.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,11 @@ void interpretert::evaluate(
389389
}
390390
else if(expr.type().id()==ID_string)
391391
{
392-
irep_idt value=to_constant_expr(expr).get_value();
393-
const char *str=value.c_str();
394-
std::size_t length=strlen(str)+1;
392+
const std::string &value = id2string(to_constant_expr(expr).get_value());
395393
if(show)
396394
warning() << "string decoding not fully implemented "
397-
<< length << eom;
398-
mp_integer tmp = get_string_container()[id2string(value)];
399-
dest.push_back(tmp);
395+
<< value.size() + 1 << eom;
396+
dest.push_back(get_string_container()[value]);
400397
return;
401398
}
402399
else

src/goto-programs/string_abstraction.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "string_abstraction.h"
1313

14-
#include <cstring>
14+
#include <algorithm>
1515

16+
#include <util/arith_tools.h>
17+
#include <util/c_types.h>
1618
#include <util/pointer_predicates.h>
1719
#include <util/std_expr.h>
1820
#include <util/std_code.h>
@@ -759,7 +761,11 @@ bool string_abstractiont::build(const exprt &object, exprt &dest, bool write)
759761

760762
if(object.id()==ID_string_constant)
761763
{
762-
mp_integer str_len=strlen(object.get(ID_value).c_str());
764+
const std::string &str_value = id2string(object.get(ID_value));
765+
// make sure we handle the case of a string constant with string-terminating
766+
// \0 in it
767+
const std::size_t str_len =
768+
std::min(str_value.size(), str_value.find('\0'));
763769
return build_symbol_constant(str_len, str_len+1, dest);
764770
}
765771

0 commit comments

Comments
 (0)