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
2 changes: 2 additions & 0 deletions src/coreclr/debug/crashreport/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CRASHREPORT_SOURCES
crashreportstringutils.cpp
signalsafeformatter.cpp
signalsafejsonwriter.cpp
signalsafeconsolewriter.cpp
inproccrashreportlifecycle.cpp
inproccrashreporter.cpp
inproccrashreportwatchdog.cpp
)
Expand Down
45 changes: 45 additions & 0 deletions src/coreclr/debug/crashreport/crashreportstringutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "crashreportstringutils.h"

#include <string.h>

void CrashReportStringUtils::CopyString(char* buffer, size_t bufferSize, const char* value)
{
if (buffer == nullptr || bufferSize == 0)
{
return;
}

if (value == nullptr)
{
buffer[0] = '\0';
return;
}

size_t toCopy = strnlen(value, bufferSize - 1);
if (toCopy != 0)
{
memcpy(buffer, value, toCopy);
}

buffer[toCopy] = '\0';
}

bool CrashReportStringUtils::AppendString(char* buffer, size_t bufferSize, size_t* pos, const char* value)
{
if (buffer == nullptr || pos == nullptr || value == nullptr || bufferSize == 0)
{
return false;
}

size_t p = *pos;
while (*value != '\0' && p + 1 < bufferSize)
{
buffer[p++] = *value++;
}
buffer[p] = '\0';
*pos = p;
return *value == '\0';
}
32 changes: 32 additions & 0 deletions src/coreclr/debug/crashreport/crashreportstringutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Shared, allocation-free, bounds-safe string helpers used by both the in-proc
// crash reporter and the crash report lifecycle. These are safe to call from the
// signal/crash path: they perform no heap allocation and never call into the
// runtime.

#pragma once

#include <stddef.h>

namespace CrashReportStringUtils
{
// Copies value into buffer, truncating if necessary, and always
// null-terminates. A null value yields an empty string. No-op if buffer is
// null or bufferSize is 0.
void CopyString(
char* buffer,
size_t bufferSize,
const char* value);

// Appends value to buffer starting at *pos, copying as much as fits, advancing
// *pos past the characters actually written, and always null-terminating.
// Returns true if the entire value fit; returns false if the value was
// truncated, the arguments are invalid, or bufferSize is 0.
bool AppendString(
char* buffer,
size_t bufferSize,
size_t* pos,
const char* value);
}
Loading