I apologize in advance for the useless title of this question, but nothing seemed to fit better.
The idea here is to replicate argv
in another variable, essentially making a copy of it. So the basic idea of what the function does is, use malloc()
to request some space for a copy and then iterate through argv
making copies of each element.
This is the code I'm working with, the development environment is right now Visual Studio 2019 (even if it's not strictly a C compiler...):
// Returns a copy of an array of strings (intended for argv, but should work with any of them):
wchar_t** copyArgv(size_t argc, wchar_t* argv[]) {
// Allocate space for the array of arguments:
wchar_t** argsCopy = malloc(((argc + 1) * sizeof(wchar_t*)));
if (!argsCopy)
return NULL;
// Copy each one of them:
for (size_t i = 0; i < argc; i++) {
argsCopy[i] = _wcsdup(argv[i]);
if (!argsCopy[i]) {
// Should also free any previous copied string I left that part out in the paste.
free(argsCopy);
return NULL;
}
}
argsCopy[argc] = NULL;
return argsCopy;
}
I've been trying different ways to make a copy of argv but each and everyone of them lets VS to believe there can be a buffer overrun when I make a copy of an argument (line:argsCopy[i] = _wcsdup(argv[i]);
) or reading invalid data in the next line, meaning reading out of the bounds of the reserved space.
All of this has lead me to believe the problem lies in the (now) only malloc()
call to reserve space for the array of arguments.
Yet I'm banging my head against the wall trying to figure out what the problem is, I mean, I think I'm asking for enough space.
I've tried other compilers as well, latest stable versions of Clang and GCC don't seem to show any such warning. So I decided to ask you, seasoned programmers, if you can spot the problem, or it's some sort of compiler bug (unlikely I bet).
For reference these are the exact warnings VS2019 is throwing (in a 64-bit compilation):
In the assignment:
Buffer overrun while writing to 'argsCopy': the writable size is '((argc+1))*sizeof(wchar_t *)' bytes, but '16' bytes might be written.
Next line, the test for NULL:
Reading invalid data from 'argsCopy': the readable size is '((argc+1))*sizeof(wchar_t *)' bytes, but '16' bytes may be read.
Also known as a buffer overrun, buffer overflow occurs when the amount of data in the buffer exceeds its storage capacity. That extra data overflows into adjacent memory locations and corrupts or overwrites the data in those locations.
The combination of memory manipulation and mistaken assumptions about the size or makeup of a piece of data is the root cause of most buffer overflows. Buffer overflow vulnerabilities typically occur in code that: Relies on external data to control its behavior.
Below are the best-known buffer overflow attacks: Stack overflow attack - This is the most common type of buffer overflow attack and involves buffer overflow in the call stack. Heap overflow attack - This type of attack targets data in the open memory pool known as the heap.
You can prevent a buffer overflow attack by auditing code, providing training, using compiler tools, using safe functions, patching web and application servers, and scanning applications.
These are warnings from the static analyzer. It tries, for example, to recognize buffer overflow situations.
Warning
It is important to note that these are warnings and not error messages. The compiler says that there might be something potentially wrong. Static analysis is generally a difficult thing.
False Positive
There is no buffer overrun situation, so it is a false positive. I would assume, that this message disappears in a future update.
Change the Code a Bit
If we change the memory allocation line as follows:
wchar_t** argsCopy = (wchar_t**)calloc(argc + 1, sizeof(wchar_t*));
then there will be no more warnings from Visual Studio 2019.
The number of bytes allocated remains the same. However, the warnings disappear.
Test
Before the change the VS Error list looks like this:
After the application of my proposed changes, the warnings have disappeared:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With