Skip to main content

Error Detection

Push Swap validates all input before attempting to sort. Invalid input triggers an error message and immediate program termination. All error handling is implemented in ft_error_exit.c and ft_fill_stack.c.

Error Output

When any error is detected, the program:
  1. Frees all allocated memory (stacks and split arrays)
  2. Writes "Error\n" to standard output
  3. Exits with status code 1
From ft_error_exit.c:45-52:
void free_errors(t_list **a, char **argv, int flag)
{
    if (flag == 1)
        free_split(argv);
    free_stack(a);
    write(1, "Error\n", 6);
    exit(1);
}
The error message is always exactly "Error\n" with a newline. No additional details are provided to stdout.

Syntax Errors

The error_syntax() function validates number format in ft_error_exit.c:54-70.

Invalid Number Format

Numbers must be valid integers with optional leading + or -:
Invalid Inputs
./push_swap 1 2 abc 3
# Error

./push_swap "12.5 3 7"
# Error

./push_swap 1 2+3 4
# Error

./push_swap "1 2 3a"
# Error

Validation Rules

From ft_error_exit.c:54-70, a string is invalid if:
  1. First character is not +, -, or a digit (0-9)
  2. Sign without digit: A + or - is not followed by a digit
  3. Non-digit characters: Any character after the first is not a digit (0-9)
./push_swap 42
./push_swap -42
./push_swap +42
./push_swap "-0" "0" "+0"

Range Errors

Numbers must fit within the INT_MIN to INT_MAX range (-2147483648 to 2147483647). From ft_fill_stack.c:87-88:
if (nbr > INT_MAX || nbr < INT_MIN)
    free_errors(a, argv, flag);

Examples of Range Errors

./push_swap 2147483648
# Error (INT_MAX + 1)

./push_swap -2147483649
# Error (INT_MIN - 1)

./push_swap 9999999999
# Error (way too large)

Valid Edge Cases

./push_swap 2147483647 -2147483648 0
# Valid - exactly at INT_MAX and INT_MIN
The program uses long internally in ft_atoi() (ft_fill_stack.c:50-74) to detect overflow before casting to int.

Duplicate Detection

The error_repetition() function prevents duplicate numbers in ft_error_exit.c:72-83.

Duplicate Numbers

./push_swap 1 2 3 2 4
# Error (2 appears twice)

./push_swap "5 3 5"
# Error (5 is duplicated)

./push_swap 0 -0 +0
# Error (all are zero)

How Duplication is Checked

From ft_error_exit.c:72-83, for each new number:
  1. The function traverses the existing stack
  2. Compares the new number against each existing node
  3. Returns 1 (error) if a match is found
  4. Returns 0 (success) if no duplicates exist
int error_repetition(t_list *stack_a, int nbr)
{
    if (!stack_a)
        return (0);
    while (stack_a)
    {
        if (stack_a->nbr == nbr)
            return (1);
        stack_a = stack_a->next;
    }
    return (0);
}

Memory Management

Push Swap guarantees proper cleanup even when errors occur.

Memory Cleanup Path

When free_errors() is called:
1

Free split array (if applicable)

If the input was a single string argument (flag == 1), free_split() releases the array created by ft_split().
2

Free stack nodes

free_stack() traverses the linked list and frees each node in ft_error_exit.c:16-31.
3

Write error message

Outputs "Error\n" to stdout.
4

Exit program

Terminates with exit code 1.

Free Stack Implementation

From ft_error_exit.c:16-31:
void free_stack(t_list **stack)
{
    t_list *tmp;
    t_list *current;

    if (!stack)
        return;
    current = *stack;
    while (current)
    {
        tmp = current->next;
        free(current);
        current = tmp;
    }
    *stack = NULL;
}
This ensures no memory leaks occur, even during error conditions.

Error Detection Flow

The validation happens in ft_fill_stack() from ft_fill_stack.c:76-94:
1

Syntax validation

error_syntax() checks if the string represents a valid integer format.
2

Range validation

After converting to long with ft_atoi(), the value is checked against INT_MIN and INT_MAX.
3

Duplicate validation

error_repetition() checks if the number already exists in the stack.
4

Add to stack

If all validations pass, append_node() adds the number to stack A.

Testing Error Handling

Validate your implementation handles all error cases:
Test Script
#!/bin/bash

# Should all output "Error" and exit with status 1

./push_swap "" 2>/dev/null || echo "Empty handled"
./push_swap "1 2 abc" 2>/dev/null || echo "Syntax error detected"
./push_swap 1 2 2 2>/dev/null || echo "Duplicate detected"
./push_swap 2147483648 2>/dev/null || echo "Overflow detected"
./push_swap 1 2 + 3 2>/dev/null || echo "Invalid sign detected"
Note: Empty string ("") is NOT an error. From main.c:22, it exits silently with status 0.

Common Error Scenarios

Scenario 1: Mixed Valid and Invalid

./push_swap 1 2 3 abc 5
Result: Error detected at “abc”, all memory freed, program exits.

Scenario 2: Duplicate After Many Valid

./push_swap 10 20 30 40 50 30
Result: Duplicate “30” detected after 5 nodes added. All 5 nodes freed, program exits.

Scenario 3: Overflow Detection

./push_swap 1 2 999999999999999
Result: Overflow detected, previous nodes freed, program exits.

Exit Codes

Exit CodeMeaning
0Success: sorted or no input
1Error: invalid input detected

Next Steps

Build docs developers (and LLMs) love