Back to all reviewers

validate inputs early

tree-sitter/tree-sitter
Based on 2 comments
C

Functions should validate input parameters at the beginning and return appropriate error indicators (like false, null, or error codes) rather than proceeding with invalid data. This prevents downstream errors and provides clear feedback to callers about invalid usage.

Error Handling C

Reviewer Prompt

Functions should validate input parameters at the beginning and return appropriate error indicators (like false, null, or error codes) rather than proceeding with invalid data. This prevents downstream errors and provides clear feedback to callers about invalid usage.

Additionally, ensure error states are properly scoped to avoid misleading results when using shared resources or global state. Error status should accurately reflect the state of the specific operation or object being queried.

Example of early input validation with error return:

bool ts_query_cursor_set_byte_range(
  TSQueryCursor *self,
  uint32_t start_byte,
  uint32_t end_byte
) {
  if (end_byte == 0) {
    end_byte = UINT32_MAX;
  } else if (start_byte > end_byte) {
    return false;  // Early validation with clear error signal
  }
  // ... proceed with valid inputs
}

This approach makes error conditions explicit and prevents functions from operating on invalid data, leading to more predictable and debuggable code.

2
Comments Analyzed
C
Primary Language
Error Handling
Category

Source Discussions