Skip to content

Commit 089397a

Browse files
committed
STR51-CPP: Address false negatives due to incomplete replace modelling
The std::string::replace function uses an internal typedef __const_iterator in libstdc++, instead of the const_iterator typedef.
1 parent bc122b7 commit 089397a

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `STR51-CPP`
2+
- Address false negatives caused by incomplete modelling of the `std::string::replace()` function.

cpp/common/src/codingstandards/cpp/standardlibrary/String.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class StdBasicString extends ClassTemplateInstantiation {
4545
Type getConstIteratorType() {
4646
exists(TypedefType t |
4747
t.getDeclaringType() = this and
48-
t.getName() = "const_iterator" and
48+
// Certain compilers user __const_iterator instead of const_iterator.
49+
t.getName() = ["const_iterator", "__const_iterator"] and
4950
result = t
5051
)
5152
}

cpp/common/test/includes/standard-library/string

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public:
4848
size_type length() const noexcept;
4949

5050
typedef __iterator<charT> iterator;
51-
typedef __iterator<const charT> const_iterator;
51+
typedef __iterator<const charT> __const_iterator;
52+
typedef __const_iterator const_iterator;
5253

5354
iterator begin();
5455
iterator end();
@@ -111,17 +112,17 @@ public:
111112
size_type n2);
112113
basic_string &replace(size_type pos, size_type n1, const charT *s);
113114
basic_string &replace(size_type pos, size_type n1, size_type n2, charT c);
114-
basic_string &replace(const_iterator i1, const_iterator i2,
115+
basic_string &replace(__const_iterator i1, __const_iterator i2,
115116
const basic_string &str);
116-
basic_string &replace(const_iterator i1, const_iterator i2, const charT *s,
117+
basic_string &replace(__const_iterator i1, __const_iterator i2, const charT *s,
117118
size_type n);
118-
basic_string &replace(const_iterator i1, const_iterator i2, const charT *s);
119-
basic_string &replace(const_iterator i1, const_iterator i2, size_type n,
119+
basic_string &replace(__const_iterator i1, __const_iterator i2, const charT *s);
120+
basic_string &replace(__const_iterator i1, __const_iterator i2, size_type n,
120121
charT c);
121122
template <class InputIterator>
122-
basic_string &replace(const_iterator i1, const_iterator i2, InputIterator j1,
123+
basic_string &replace(__const_iterator i1, __const_iterator i2, InputIterator j1,
123124
InputIterator j2);
124-
basic_string &replace(const_iterator, const_iterator,
125+
basic_string &replace(__const_iterator, __const_iterator,
125126
initializer_list<charT>);
126127

127128
size_type copy(charT *s, size_type n, size_type pos = 0) const;

0 commit comments

Comments
 (0)