Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RymlDocument : public Document
public:
static std::unique_ptr<Document> Parse(const std::string &source, const std::string &content);

RymlDocument() {}
RymlDocument() : event_handler_(MakeCallbacks()) {}
RymlDocument(RymlDocument &&) = delete;
RymlDocument(const RymlDocument &) = delete;
RymlDocument &operator=(RymlDocument &&) = delete;
Expand All @@ -36,6 +36,12 @@ class RymlDocument : public Document
DocumentNodeLocation Location(ryml::ConstNodeRef node) const;

private:
static ryml::Callbacks MakeCallbacks();
[[noreturn]] static void OnError(const char *msg,
size_t msg_len,
ryml::Location location,
void *user_data);

ryml::ParserOptions opts_;
ryml::Parser::handler_type event_handler_;
std::unique_ptr<ryml::Parser> parser_;
Expand Down
26 changes: 26 additions & 0 deletions sdk/src/configuration/ryml_document.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <stddef.h>
#include <exception>
#include <memory>
#include <ostream>
Expand All @@ -11,6 +12,7 @@
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/configuration/document.h"
#include "opentelemetry/sdk/configuration/document_node.h"
#include "opentelemetry/sdk/configuration/invalid_schema_exception.h"
#include "opentelemetry/sdk/configuration/ryml_document.h"
#include "opentelemetry/sdk/configuration/ryml_document_node.h"
#include "opentelemetry/version.h"
Expand All @@ -21,6 +23,30 @@ namespace sdk
namespace configuration
{

// Custom ryml error callback that throws instead of calling abort().
// This ensures the try-catch in ParseDocument works regardless of how
// ryml was compiled (with or without RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS).
void RymlDocument::OnError(const char *msg,
size_t msg_len,
ryml::Location location,
void * /*user_data*/)
{
DocumentNodeLocation loc;
loc.offset = location.offset;
loc.line = location.line;
loc.col = location.col;
loc.filename = std::string(location.name.str, location.name.len);

throw InvalidSchemaException(loc, std::string(msg, msg_len));
}

ryml::Callbacks RymlDocument::MakeCallbacks()
{
ryml::Callbacks cb = ryml::get_callbacks();
cb.m_error = &RymlDocument::OnError;
return cb;
}

std::unique_ptr<Document> RymlDocument::Parse(const std::string &source, const std::string &content)
{
auto doc = std::make_unique<RymlDocument>();
Expand Down
23 changes: 23 additions & 0 deletions sdk/test/configuration/yaml_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,26 @@ file_format: "1.0"
auto config = DoParse(yaml);
ASSERT_EQ(config, nullptr);
}

TEST(Yaml, malformed_yaml)
{
// "headers" is indented under "client_certificate" instead of "otlp_http"
std::string yaml = R"(
file_format: "1.0"
tracer_provider:
processors:
- simple:
exporter:
otlp_http:
endpoint: http://localhost:4318/v1/traces
certificate: /app/cert.pem
client_key: /app/cert.pem
client_certificate: /app/cert.pem
headers:
- name: header_name
value: header_value
Comment thread
perhapsmaple marked this conversation as resolved.
)";

auto config = DoParse(yaml);
ASSERT_EQ(config, nullptr);
}
Loading