Saturate return value in builtin_set_query#7698
Saturate return value in builtin_set_query#7698ethulhu wants to merge 1 commit intofish-shell:masterfrom
Conversation
Considering that the shared utility function would be about the same amount of code I don't think it helps. Now, if the builtin return type could be some saturating one or something that might be of use, but tbh it still feels like overkill. |
builtin_set_query returns the number of missing variables. Because the return value passed to the shell is an 8-bit unsigned integer, if the number of missing variables is a multiple of 256, it would overflow to 0. This commit saturates the return value at 255 if there are more than 255 missing variables.
c807f7c to
b280ede
Compare
|
Merged as 6dd6a57 (with the CHANGELOG changed to 3.2.0). Thanks! |
It could also be implemented higher up the callstack, e.g. in For example, putting the saturation check into /// Construct directly from an exit code.
static proc_status_t from_exit_code(int ret) {
// Some paranoia.
constexpr int zerocode = w_exitcode(0, 0);
static_assert(WIFEXITED(zerocode), "Synthetic exit status not reported as exited");
if (ret > 255) {
ret = 255;
}
return proc_status_t(w_exitcode(ret, 0 /* sig */));
} |
|
I'd either move the saturation logic to Maybe adding an |
Like this? master...ethulhu:saturate-all-builtins (not a PR because I've not changed any documentation yet) |
|
Looking through the documented exit codes, I think we have four affected builtins:
Anyway, your change looks good and is maybe safer, so feel free to go for it. |
I agree that I've filled in the branch I linked earlier, and made it into PR #7702. |
Description
builtin_set_queryreturns the number of missing variables. Because the return value passed to the shell is an 8-bit unsigned integer, if the number of missing variables is a multiple of 256, prior to this PR it would overflow to 0.This PR saturates the return value at 255 if there are more than 255 missing variables.
This issue also seems to apply to
functions --queryand probably others; this PR only fixesset --queryfor now, but could be expanded to fix others.Currently, it is fixed with an ad-hoc
if-statement, but it could be fixed with some shared utility function likeint saturate_error_count_to_255(int):TODOs: