Skip to content

Report an out-of-range prefix length in C tests - #384

Merged
jserv merged 1 commit into
sysprog21:mainfrom
ericodingTWICEaWEAK:fix-382-c-prefix-length
Oct 21, 2026
Merged

jserv merged 1 commit into
sysprog21:mainfrom
ericodingTWICEaWEAK:fix-382-c-prefix-length

Conversation

@ericodingTWICEaWEAK

@ericodingTWICEaWEAK ericodingTWICEaWEAK commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

The C harness for in-place prefix exercises such as remove-element and remove-duplicates-from-sorted-array clamped a negative or oversized returned length to 0 and printed [], so a solution that always returns -1 passed the cases whose expected result is empty and an oversized length produced no explanation.

The harness now reports Return value must be an integer length for the kept prefix. as an error result when the length is outside 0..numsSize, the same diagnostic and result shape as the C++, Java, JavaScript and Python harnesses, and the bounds check runs before json_int_array so the buffer is never read past its size.

A unit test in tests/browser/compiler-explorer.test.js asserts that the generated C harness contains the bounds guard and the error result, next to the existing Java assertion for remove-element.

Closes #382

Comment thread web/compiler-explorer.js Outdated
cubic-dev-ai[bot]

This comment was marked as resolved.

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Avoid commit messages such as Update web/compiler-explorer.js, as Git already tracks which files have changed.

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please squash your commits.

@ericodingTWICEaWEAK

Copy link
Copy Markdown
Contributor Author

Done. I've squashed my commits into a single commit. Thanks!

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Don't leave this in:

Update web/compiler-explorer.js

Co-authored-by: xxx

The C harness clamped a returned prefix length that was negative or
larger than the input array to zero. A solution that always returned
-1 therefore passed every case whose expected result is empty, and an
oversized length showed up as an empty array instead of an error.
Print the same diagnostic the C++, Java, JavaScript and Python
harnesses already use, and keep the bounds check ahead of the array
print so the harness never reads past the buffer. The cost is a
longer generated C statement in the harness template.
@ericodingTWICEaWEAK

Copy link
Copy Markdown
Contributor Author

Done. I've removed them. Thanks!

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, Let's wait for the CI.

@jserv
jserv merged commit f3ae86c into sysprog21:main Oct 21, 2026
10 checks passed
@jserv

jserv commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Thank @ericodingTWICEaWEAK for contributing!

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.

C tests silently clamp out-of-bounds prefix lengths to zero instead of reporting an error

3 participants