That depends. The OP's title merely references defining the behavior.
Vimscript could be defined to have wrapping arithmetic operations and
this discussion would then be over.
The other option, which seems like it is more in the spirit of the
body of the original post, and which eliminates the word "undefined"
per the C standard, involves trapping integer overflow and underflow
or ensuring overflow or underflow never occurs. This can be done in
the following ways:
1) For some POSIX systems using GCC, it might be necessary to wait
until the bug with "-ftrapv" is fixed and and the patch distributed.
2) For some POSIX systems using GCC nothing needs to be done and
"-ftrapv" is usable.
3) For all(?) POSIX systems the combination of "-fsanitize=undefined
-fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-error"
works despite "-ftrapv" not working.
4) For Windows it is possible to generate an SEH exception on overflow
or underflow, however I'm not sure what other facilities expose this
functionality.
5) For all supported systems explicit range checks could be done for
vimscript arithmetic.
6) For all supported systems a large number library could be used to
eliminate the possibility of overflow or underflow.
With respect to the combination of 1, 2, and 3, some reading on the
issue (
http://blog.robertelder.org/gcc-signed-overflow-trapping-ftrapv-doesnt-work/)
implies that newer versions of GCC might not suffer from the "-ftrapv"
problem. It may be a better idea to use the flags given in item 3
anyway, as they seem to be newer; unfortunately that generates SIGILL
instead of SIGABRT and I can't find a way to recover from that signal
in a portable way.
My only comment on 4 is that some programs are unable to use SEH, and
vim is likely among them, but someone else would have to comment.
5 and 6 are likely the most work, but also provide the most
flexibility. 6 might eventually be a goal anyway, but I'm not aware of
anyone having problems with the range of the datatypes in vimscript.
That still leaves the question of what happens when overflow or
underflow occurs. If overflow and underflow are disallowed, clamping
the value and continuing execution is the only choice I'm aware of
that doesn't involve throwing an exception or halting execution - not
that those are unreasonable choices.
As to implementation, the first three (or four) items would require
creating the signal handler and exposing the vimscript interpreter
internals to it. Comments on the best way to do that are very welcome,
I'm not too familiar with the Vim codebase. Five would simply involve
adding the range checks manually and reporting an error (or clamping
the values) when exceptional arithmetic occurs.
Or, *instead of all of the above,* arithmetic can be defined to wrap.
Technically, with no other changes, this doesn't solve the original
complaint about undefined behavior - as it is left undefined in the C
standard, Vim could conceivably be ported to a system which does wrap
exceptional arithmetic by default.