1 | initial version |
argv[3]
is a pointer, it is NOT a char array to store a string in. And argv is declared as char* argv[]
, which means "an array of pointers to char".
For example argv[1] = "-Tjson";
means: set the pointer argv[1] to the memory address where the string "-Tjson" is stored. That is how pointers in C work.
So, by writing to unspecified memory using sprintf
the memory gets corrupted causing a crash further down te stream...
Allocate some memory first, e.g. on the stack using char buf[50];
and set the pointer argv[3] = buf;
then inside the (for) loop fill the string buffer snprintf(buf, sizeof buf, "D:\\Windows\\files\\packet%d.pcap", i);
2 | No.2 Revision |
argv[3]
is a pointer, it is NOT a char array to store a string in. And argv is declared as char* argv[]
, which means "an array of pointers to char".
For example argv[1] = "-Tjson";
means: set the pointer argv[1] to the memory address where the string "-Tjson" is stored. That is how pointers in C work.
So, by writing to unspecified memory using sprintf
the memory gets corrupted causing a crash further down te the stream...
Allocate some memory first, e.g. on the stack using char buf[50];
and set the pointer argv[3] = buf;
then inside the (for) loop fill the string buffer snprintf(buf, sizeof buf, "D:\\Windows\\files\\packet%d.pcap", i);
3 | No.3 Revision |
argv[3]
is a pointer, it is NOT a char array to store a string in. And argv is declared as char* argv[]
, which means "an array of pointers to char".
For example argv[1] = "-Tjson";
means: set the pointer argv[1] to the memory address where the string "-Tjson" is stored. That is how pointers in C work.
So, by writing to unspecified memory using sprintf
the memory gets corrupted causing a crash further down the stream...
Allocate some memory first, e.g. on the stack using char buf[50];
and set the pointer argv[3] = buf;
then inside the (for) loop fill the string buffer snprintf(buf, sizeof buf, "D:\\Windows\\files\\packet%d.pcap", i);
But this whole peace of code looks awkward to me. Why would you require 3 command line arguments so you can replace them with your own, instead of calling tshark 3 times (or use your own array)?
The size of the array argv
is set by the caller of main, so if you do not provide any command line arguments the size is 1 (or 2 for a NULL pointer as sentinel). In that case argv[3]
is beyond the memory reserved for the array. That can also cause problems. In C it is the responsibility of the programmer to check the array bounds. There is no implicit memory allocation in C programming language (therefore also no implicit memory release by a process called ‘Garbage Collection’) and that is why working with character strings is hard.