Skip to content
Open
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
28 changes: 18 additions & 10 deletions include-c/datadog/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

// These values establish the size of string buffers in the C API; they do not imply
// that the Datadog platform imposes any such limits
#define DATADOG_MAX_SERVICE_NAME_LEN 127
#define DATADOG_MAX_APPLICATION_STORAGE_PATH_LEN 511
#define DATADOG_MAX_CLIENT_TOKEN_LEN 63
#define DATADOG_MAX_SERVICE_LEN 127
#define DATADOG_MAX_ENV_LEN 127
#define DATADOG_MAX_VERSION_LEN 127
#define DATADOG_MAX_VARIANT_LEN 127
#define DATADOG_INTERNAL_MAX_CUSTOM_ENDPOINT_URL_LEN 255
#define DATADOG_INTERNAL_MAX_SOURCE_LEN 15
#define DATADOG_INTERNAL_MAX_SDK_VERSION_LEN 31

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -130,9 +138,9 @@ typedef enum {
*/
typedef struct dd_internal_options {
bool flush_http_requests_on_stop;
const char* custom_endpoint_url;
const char* source;
const char* sdk_version;
char custom_endpoint_url[DATADOG_INTERNAL_MAX_CUSTOM_ENDPOINT_URL_LEN + 1];
char source[DATADOG_INTERNAL_MAX_SOURCE_LEN + 1];
char sdk_version[DATADOG_INTERNAL_MAX_SDK_VERSION_LEN + 1];
} dd_internal_options_t;

/**
Expand All @@ -145,13 +153,13 @@ typedef struct dd_core_config {
void* diagnostic_handler_userdata;
dd_diagnostic_level_t diagnostic_threshold;
dd_tracking_consent_t tracking_consent;
char application_storage_path[512];
char application_storage_path[DATADOG_MAX_APPLICATION_STORAGE_PATH_LEN + 1];
dd_site_t site;
const char* client_token;
const char* service;
const char* env;
const char* application_version;
const char* variant;
char client_token[DATADOG_MAX_CLIENT_TOKEN_LEN + 1];
char service[DATADOG_MAX_SERVICE_LEN + 1];
char env[DATADOG_MAX_ENV_LEN + 1];
char application_version[DATADOG_MAX_VERSION_LEN + 1];
char variant[DATADOG_MAX_VARIANT_LEN + 1];
dd_batch_size_t batch_size;
dd_upload_frequency_t upload_frequency;
dd_batch_processing_level_t batch_processing_level;
Expand Down
6 changes: 3 additions & 3 deletions include-c/datadog/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef enum {
typedef struct dd_logger_config {
uint32_t version;
float remote_sample_rate;
char service[DATADOG_MAX_SERVICE_NAME_LEN + 1];
char service[DATADOG_MAX_SERVICE_LEN + 1];
char name[DATADOG_MAX_LOGGER_NAME_LEN + 1];
dd_log_level_t remote_log_threshold;
size_t initial_attribute_capacity;
Expand All @@ -69,8 +69,8 @@ DATADOG_API void dd_logger_config_set_remote_sample_rate(
* logger will use the service name configured globally via dd_core_config_t.
*
* Config stores a copy of provided string value. If the given string value exceeds
* DATADOG_MAX_SERVICE_NAME_LEN, it will be truncated to that length. Both NULL and ""
* will be interepreted as no value, causing the logger to use the default service name.
* DATADOG_MAX_SERVICE_LEN, it will be truncated to that length. Both NULL and "" will
* be interepreted as no value, causing the logger to use the default service name.
*/
DATADOG_API void dd_logger_config_set_service(
dd_logger_config_t* config, const char* value
Expand Down
72 changes: 72 additions & 0 deletions src/datadog/c/config_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.

#pragma once

#include <cstddef>
#include <cstdio>
#include <cstring>

#include "datadog/impl/core/util/diagnostics.hpp"

// Expands a macro constant to a string literal, e.g. DATADOG_CSTR(63) -> "63".
// Used to embed DATADOG_MAX_* limit values in static diagnostic message strings
// without any runtime formatting.
#define DATADOG_CSTR_(x) #x
#define DATADOG_CSTR(x) DATADOG_CSTR_(x)

/**
* Copies `src` into the fixed-size buffer `dest[0..dest_size)`, truncating if
* the source string is too long. If truncation occurs, emits `truncation_msg`
* at warning level via `logger`. Both `src` and `truncation_msg` may be null:
* a null or empty `src` writes an empty string; a null `truncation_msg` skips
* the diagnostic (intended for callers with no diagnostic handler available).
*/
static inline void assign_string_truncate(
char* dest,
size_t dest_size,
const char* src,
const datadog::impl::DiagnosticLogger& logger,
const char* truncation_msg
) {
if (!src || src[0] == '\0') {
dest[0] = '\0';
return;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
int written = std::snprintf(dest, dest_size, "%s", src);
if (truncation_msg && written >= 0 && static_cast<size_t>(written) >= dest_size) {
logger.Warning(truncation_msg);
}
}

/**
* Copies `src` into the fixed-size buffer `dest[0..dest_size)`, refusing if
* the source string would overflow the buffer. A null or empty `src` writes an
* empty string. If the string is too long, emits `overflow_msg` at error level
* via `logger` and leaves `dest` unchanged; pass a null `overflow_msg` to
* suppress the diagnostic.
*/
static inline void assign_string_strict(
char* dest,
size_t dest_size,
const char* src,
const datadog::impl::DiagnosticLogger& logger,
const char* overflow_msg
) {
if (!src || src[0] == '\0') {
dest[0] = '\0';
return;
}
if (!std::memchr(src, '\0', dest_size)) {
if (overflow_msg) {
logger.Error(overflow_msg);
}
return;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
std::snprintf(dest, dest_size, "%s", src);
}
Loading