Skip to content

Commit 90da327

Browse files
authored
fix: fix possible unwrappered exception throwing when sw8 contains illegal span id (#120)
Signed-off-by: wangbaiping <[email protected]>
1 parent 20ad7cd commit 90da327

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

source/propagation_impl.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <memory>
1919

2020
#include "absl/memory/memory.h"
21+
#include "absl/strings/numbers.h"
2122
#include "absl/strings/string_view.h"
2223
#include "cpp2sky/exception.h"
2324
#include "source/utils/base64.h"
@@ -60,15 +61,20 @@ SpanContextImpl::SpanContextImpl(absl::string_view header_value) {
6061

6162
if (fields[0] != "0" && fields[0] != "1") {
6263
throw TracerException(
63-
"Invalid span context format. sample field must be 0 or 1.");
64+
"Invalid span context format. Sample field must be 0 or 1.");
6465
}
6566

6667
// Sampling is always true
6768
sample_ = true;
6869
trace_id_ = Base64::decodeWithoutPadding(absl::string_view(fields[1]));
6970
trace_segment_id_ =
7071
Base64::decodeWithoutPadding(absl::string_view(fields[2]));
71-
span_id_ = std::stoi(fields[3]);
72+
73+
if (!absl::SimpleAtoi(fields[3], &span_id_)) {
74+
throw TracerException(
75+
"Invalid span id format. Span id field must be integer number.");
76+
}
77+
7278
service_ = Base64::decodeWithoutPadding(absl::string_view(fields[4]));
7379
service_instance_ =
7480
Base64::decodeWithoutPadding(absl::string_view(fields[5]));

source/propagation_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class SpanContextImpl : public SpanContext {
4242
private:
4343
// Based on
4444
// https://github.com/apache/skywalking/blob/master/docs/en/protocols/Skywalking-Cross-Process-Propagation-Headers-Protocol-v3.md
45-
bool sample_ = true;
45+
bool sample_{true};
4646
std::string trace_id_;
4747
std::string trace_segment_id_;
48-
int32_t span_id_;
48+
int32_t span_id_{};
4949
std::string service_;
5050
std::string service_instance_;
5151
std::string endpoint_;

test/propagation_test.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ static constexpr absl::string_view invalid_sample =
3636
"3-MQ==-NQ==-3-bWVzaA==-aW5zdGFuY2U=-L2FwaS92MS9oZWFsdGg=-"
3737
"ZXhhbXBsZS5jb206ODA4MA==";
3838

39+
static constexpr absl::string_view invalid_span_id =
40+
"1-MQ==-NQ==-abc-bWVzaA==-aW5zdGFuY2U=-L2FwaS92MS9oZWFsdGg=-"
41+
"ZXhhbXBsZS5jb206ODA4MA==";
42+
3943
TEST(TestSpanContext, Basic) {
4044
auto data = std::string(sample.data());
4145
SpanContextImpl sc(data);
@@ -62,6 +66,7 @@ TEST(TestSpanContext, MalformedSpanContext) {
6266
auto data = std::string(invalid_sample.data());
6367
EXPECT_THROW(SpanContextImpl{data}, TracerException);
6468
}
69+
{ EXPECT_THROW(SpanContextImpl{invalid_span_id}, TracerException); }
6570
}
6671

6772
} // namespace cpp2sky

0 commit comments

Comments
 (0)