Skip to content

Commit

Permalink
make CError less error prone and increase max size to 2k
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Oct 25, 2023
1 parent b6ade5d commit 3f41c3b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
19 changes: 13 additions & 6 deletions crates/rerun_c/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use crate::{CError, CErrorCode};

impl CError {
/// The maximum size in bytes of the [`CError::message`] field.
///
/// Error message larger than this value will be automatically truncated.
//
// NOTE: You must update `rr_error.description` too if you modify this value.
pub const MAX_MESSAGE_SIZE_BYTES: usize = 2048;

pub const OK: CError = CError {
code: CErrorCode::Ok,
message: [0; 512],
message: [0; Self::MAX_MESSAGE_SIZE_BYTES],
};

pub fn new(code: CErrorCode, message: &str) -> Self {
let mut message_c = [0; 512];
let mut message_c = [0; Self::MAX_MESSAGE_SIZE_BYTES];

// Copy string character by character.
// Ensure that when truncating is necessary, we don't truncate in the middle of a UTF-8 character!
Expand Down Expand Up @@ -77,7 +84,7 @@ mod tests {
#[allow(unsafe_code)]
fn write_error_handles_message_overflow() {
// With ASCII character.
let description = "a".repeat(1024);
let description = "a".repeat(CError::MAX_MESSAGE_SIZE_BYTES * 2);
let error = CError::new(CErrorCode::Ok, &description);
let num_expected_bytes = error.message.len() - 1;
assert_eq!(
Expand All @@ -86,7 +93,7 @@ mod tests {
);

// With 2 byte UTF8 character
let description = "œ".repeat(1024);
let description = "œ".repeat(CError::MAX_MESSAGE_SIZE_BYTES * 2);
let error = CError::new(CErrorCode::Ok, &description);
let num_expected_bytes = ((error.message.len() - 1) / 2) * 2;
assert_eq!(
Expand All @@ -95,7 +102,7 @@ mod tests {
);

// With 3 byte UTF8 character
let description = "∂".repeat(1024);
let description = "∂".repeat(CError::MAX_MESSAGE_SIZE_BYTES * 2);
let error = CError::new(CErrorCode::Ok, &description);
let num_expected_bytes = ((error.message.len() - 1) / 3) * 3;
assert_eq!(
Expand All @@ -104,7 +111,7 @@ mod tests {
);

// With 4 byte UTF8 character
let description = "😀".repeat(1024);
let description = "😀".repeat(CError::MAX_MESSAGE_SIZE_BYTES * 2);
let error = CError::new(CErrorCode::Ok, &description);
let num_expected_bytes = ((error.message.len() - 1) / 4) * 4;
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion crates/rerun_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub enum CErrorCode {
#[derive(Clone)]
pub struct CError {
pub code: CErrorCode,
pub message: [c_char; 512],
pub message: [c_char; Self::MAX_MESSAGE_SIZE_BYTES],
}

// ----------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion crates/rerun_c/src/rerun.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ typedef struct rr_error {
rr_error_code code;

/// Human readable description of the error in null-terminated UTF8.
char description[512];
//
// NOTE: You must update `CError::MAX_MESSAGE_SIZE_BYTES` too if you modify this value.
char description[2048];
} rr_error;

// ----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/c/spawn_viewer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdio.h>

int main(void) {
rr_error error = {0};
rr_error error = {};
rr_spawn(NULL, &error);

if (error.code != 0) {
Expand Down
7 changes: 6 additions & 1 deletion rerun_cpp/src/rerun/c/rerun.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions rerun_cpp/src/rerun/recording_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ namespace rerun {
/// Spawns a new Rerun Viewer process from an executable available in PATH, then connects to it
/// over TCP.
///
/// This function returns immediately.
/// If a Rerun Viewer is already listening on this TCP port, the stream will be redirected to
/// that viewer instead of starting a new one.
///
/// ## Parameters
///
Expand All @@ -138,11 +139,11 @@ namespace rerun {
/// dropping data if progress is not being made. Passing a negative value indicates no
/// timeout, and can cause a call to `flush` to block indefinitely.
Error spawn(
uint16_t port = 9876, //
const char* memory_limit = "75%", //
const char* executable_name = nullptr, //
const char* executable_path = nullptr, //
float flush_timeout_sec = 2.0 //
uint16_t port = 0, // defer to default value
const char* memory_limit = nullptr, // defer to default value
const char* executable_name = nullptr, // defer to default value
const char* executable_path = nullptr, // defer to default value
float flush_timeout_sec = 2.0
);

/// Stream all log-data to a given file.
Expand Down
11 changes: 7 additions & 4 deletions rerun_cpp/src/rerun/spawn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace rerun {
/// Spawns a new Rerun Viewer process from an executable available in PATH, ready to
/// listen for incoming TCP connections.
///
/// If a Rerun Viewer is already listening on this TCP port, the stream will be redirected to
/// that viewer instead of starting a new one.
///
/// ## Parameters
///
/// port:
Expand All @@ -27,9 +30,9 @@ namespace rerun {
/// Enforce a specific executable to use instead of searching though PATH
/// for [`Self::executable_name`].
Error spawn(
uint16_t port = 9876, //
const char* memory_limit = "75%", //
const char* executable_name = nullptr, //
const char* executable_path = nullptr //
uint16_t port = 0, // defer to default value
const char* memory_limit = nullptr, // defer to default value
const char* executable_name = nullptr, // defer to default value
const char* executable_path = nullptr // defer to default value
);
} // namespace rerun

0 comments on commit 3f41c3b

Please sign in to comment.