Tag Info

New answers tagged

5

Technically, such checking requires an additional step. You have to list all the parameters, and compare this list with the list of allowed ones. Then, if you decide to accept an additional parameter, you have to change this list, requiring even more work. All this code has to be tested as well. What's the benefit? The benefit is that a programmer who made ...


4

A function that receives a pointer does not know of the length of the corresponding array. You must pass in as a parameter yourself explicitly: void myFunc(int *yourArray, size_t yourArrayLen) Once you've done that, throwing an error is trivial. Of course, this still leaves the possibility that your caller might give you the wrong length. You can't ...


4

The answer is really fairly simple: if you want safety, use something that actually provides it--and that's not C, and not raw C-style arrays. Without departing too far from the basic style of C and raw arrays, you can use C++ and an std::vector with [i] replaced by .at(i), and get bounds checking. Using std::vector instead makes most of the problems with ...


0

Use a custom wrapper for malloc (or write your own) that keeps additional information about the blocks it allocates. The one I use adds a few "guard bytes" to every allocation, embeds the length of the allocation as the a[-1], and checks the guard bytes and other things upon deallocation.


1

There is no way in C(++) to get the length of an array from a pointer to its first element. (There are platform-specific functions like _msize in MSVCRT, but that only works on malloced pointers.) What's typically done when passing arrays to functions is to pass the length along with the pointer so that bounds-checking can be done at runtime. void ...


7

Checking array bounds like you want is implementation specific, because buffer overflow is an example of undefined behavior (and this explains why UB can be really bad). It is also an undecidable problem in general. You can easily show that statically finding (by static program analysis, e.g. of the C++ source code, without actually running the program) ...


3

Is putting stack trace in files is bad? Since writing to files has nothing to do with UX, is just that security issue that makes it look bad idea? No it is not a bad thing. It is a good thing. Fatal errors need consistent and clear logs and information to attempt to reproduce the error, verify the error seen, triage, resolution and then testing. ...



Top 50 recent answers are included