Skip to content

Conversation

DariaKunoichi
Copy link
Contributor

No description provided.

* Async-safe number-to-string conversion

* Changes requested in code review

---------

Co-authored-by: Robert <[email protected]>
@GLinnik21
Copy link
Collaborator

What about places like

static void writeBasicRegisters(const KSCrashReportWriter *const writer, const char *const key,
const struct KSMachineContext *const machineContext)
{
char registerNameBuff[30];
const char *registerName;
writer->beginObject(writer, key);
{
const int numRegisters = kscpu_numRegisters();
for (int reg = 0; reg < numRegisters; reg++) {
registerName = kscpu_registerName(reg);
if (registerName == NULL) {
snprintf(registerNameBuff, sizeof(registerNameBuff), "r%d", reg);
registerName = registerNameBuff;
}
writer->addUIntegerElement(writer, registerName, kscpu_registerValue(machineContext, reg));
}
}
writer->endContainer(writer);
}

writeBasicRegisters can also be called from a signal handler and it still uses snprintf.

Comment on lines +908 to +909
memcpy(nameBuffer, "stack@0x", 8);
char *addressStart = nameBuffer + 8;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use sizeeof to make it more error-proof?

Suggested change
memcpy(nameBuffer, "stack@0x", 8);
char *addressStart = nameBuffer + 8;
static const char prefix[] = "stack@0x";
memcpy(nameBuffer, prefix, sizeof(prefix) - 1);
char *addressStart = nameBuffer + sizeof(prefix) - 1;

@GLinnik21 GLinnik21 requested a review from Copilot August 3, 2025 23:58
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces async-safe number to string conversion functions to replace unsafe system calls like sprintf in crash reporting contexts. The implementation adds custom hex and UUID string conversion functions that avoid potential deadlocks during crash handling.

  • Adds KSStringConversion module with async-safe uint64_to_hex and uuid_to_string functions
  • Replaces sprintf call in crash reporting with the new async-safe hex conversion
  • Includes comprehensive unit tests and integration tests for the new functionality

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Sources/KSCrashRecordingCore/include/KSStringConversion.h Header defining async-safe string conversion functions
Sources/KSCrashRecordingCore/KSStringConversion.c Implementation of hex and UUID string conversion utilities
Sources/KSCrashRecording/KSCrashReportC.c Updates crash reporting to use async-safe hex conversion instead of sprintf
Tests/KSCrashRecordingCoreTests/KSStringConversion_Tests.m Unit tests for the new string conversion functions
Sources/KSCrashCore/include/KSCrashNamespace.h Namespace registration for new functions
Samples/Tests/IntegrationTests.swift Integration test validating memory introspection with new formatting
Samples/Tests/Core/PartialCrashReport.swift Test model updates to support notable addresses validation
Samples/Common/Sources/IntegrationTestsHelper/InstallConfig.swift Test configuration support for memory introspection

if (segmentIndex != 4) {
memcpy(currentDst++, "-", 1);
}
}
Copy link
Preview

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function kssc_uuid_to_string does not null-terminate the output string. The destination buffer should have a null terminator added after the UUID string is written.

Suggested change
}
}
*currentDst = '\0';

Copilot uses AI. Check for mistakes.

sprintf(nameBuffer, "stack@%p", (void *)address);
memcpy(nameBuffer, "stack@0x", 8);
char *addressStart = nameBuffer + 8;
kssc_uint64_to_hex((uintptr_t)address, addressStart, 1, false);
Copy link
Preview

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output from kssc_uint64_to_hex is not null-terminated in the destination buffer. Since nameBuffer is used as a string, it needs proper null termination after the hex conversion.

Suggested change
kssc_uint64_to_hex((uintptr_t)address, addressStart, 1, false);
int hexLen = kssc_uint64_to_hex((uintptr_t)address, addressStart, 1, false);
nameBuffer[8 + hexLen] = '\0';

Copilot uses AI. Check for mistakes.

currentDst += 2;
}
if (segmentIndex != 4) {
memcpy(currentDst++, "-", 1);
Copy link
Preview

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using memcpy to copy a single character is unnecessary. A simple assignment *currentDst++ = '-'; would be more efficient and clearer.

Suggested change
memcpy(currentDst++, "-", 1);
*currentDst++ = '-';

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants