UDP

I'm creating a udp server code in C++ but I keep getting an error in my if statement with "sscantf". I keep getting an error and warning saying that it is not "safe..". I don't get why it's saying that when that is the only thing I can use for this part.

if (argc != 3 && argc != 4)
{
usage();
}
if (sscanf(argv[1], "%d.%d.%d.%d", &a1, &a2, &a3, &a4) != 4)
{
usage();
}
if (sscanf(argv[2], "%u", &port_number) != 1)
{
usage();
}
if (argc == 4)
{
if (sscanf(argv[3], "%d.%d.%d.%d", &b1, &b2, &b3, &b4) != 4)
{
usage();
}
}
I keep getting an error and warning saying that it is not "safe"

It's not safe -- especially in this case where the argument is not formatted -- it comes straight from user-input. Instead, Microsoft wants you to use the non-standard sscanf_s.

It has been several years since I used Windows, but IIRC you can disable that warning by defining a certain macro - I believe it is referenced in the error message.

I don't get why it's saying that when that is the only thing I can use for this part.

Why do you think that?
you can use c++ strings just as easily here. Or stringstreams. Or atof / atoi. Or several other things.

string s;
if()
s = argv[1];
..

if()
s = argv[2];
...

Last edited on
Topic archived. No new replies allowed.