Skip to content

Fix mp_hal_stdout_tx_strn(): use ssize_t, loop for partial writes, handle EINTR#4

Merged
morita5840 merged 2 commits intofix/mphal-stdout-tx-strn-signaturefrom
copilot/sub-pr-2-again
Mar 21, 2026
Merged

Fix mp_hal_stdout_tx_strn(): use ssize_t, loop for partial writes, handle EINTR#4
morita5840 merged 2 commits intofix/mphal-stdout-tx-strn-signaturefrom
copilot/sub-pr-2-again

Conversation

Copy link

Copilot AI commented Mar 21, 2026

write() returns ssize_t, but the result was stored in int and returned as mp_uint_t, causing silent truncation of large write counts on 64-bit systems. There was also no handling of partial writes or EINTR, meaning stdout output could be silently dropped.

Changes

  • bricks/ev3dev/ev3dev_mphal.c
    • Changed ret type from int to ssize_t
    • Added write loop to ensure all bytes are flushed, advancing str pointer on partial writes
    • Retry on EINTR; return bytes written so far on other errors
    • Defensive guard against ret > remaining to prevent unsigned underflow (POSIX-impossible, but explicit)
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
    size_t remaining = len;
    while (remaining > 0) {
        MP_THREAD_GIL_EXIT();
        ssize_t ret = write(STDOUT_FILENO, str, remaining);
        MP_THREAD_GIL_ENTER();
        if (ret < 0) {
            if (errno == EINTR) {
                continue;
            }
            return len - remaining;
        }
        /* Defensive check: POSIX guarantees ret <= remaining, but guard against non-compliant implementations. */
        if ((size_t)ret > remaining) {
            return len;
        }
        str += ret;
        remaining -= (size_t)ret;
    }
    return len;
}

⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copilot AI changed the title [WIP] [WIP] Address feedback on signature mismatch in mp_hal_stdout_tx_strn() fix Fix mp_hal_stdout_tx_strn(): use ssize_t, loop for partial writes, handle EINTR Mar 21, 2026
Copilot AI requested a review from morita5840 March 21, 2026 10:58
@morita5840 morita5840 marked this pull request as ready for review March 21, 2026 10:58
@morita5840 morita5840 merged commit b95d98d into fix/mphal-stdout-tx-strn-signature Mar 21, 2026
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.

2 participants