CSecurity
asked on
Double sscanf confilicts, overwrites over variables
Hi
When I run this code it overwrites second parameter over first ones:
unsigned char test1[4];
unsigned char test2[4];
memset(test1, 0, 4);
sscanf(param2, "%d.%d.%d.%d", &(test1[0]), &(test1[1]), &(test1[2]), &(test1[3]));
memset(test2, 0, 4);
sscanf(param1, "%d.%d.%d.%d", &(test2[0]), &(test2[1]), &(test2[2]), &(test2[3]));
When I debug, when I reach sscanf sscanf(param2...) it parses test1 properly and I have proper data in test1
When I reach and finish execution of scanf(param1...) it overwrites data and makes test1 data 0
Please advice.
Thanks from now
When I run this code it overwrites second parameter over first ones:
unsigned char test1[4];
unsigned char test2[4];
memset(test1, 0, 4);
sscanf(param2, "%d.%d.%d.%d", &(test1[0]), &(test1[1]), &(test1[2]), &(test1[3]));
memset(test2, 0, 4);
sscanf(param1, "%d.%d.%d.%d", &(test2[0]), &(test2[1]), &(test2[2]), &(test2[3]));
When I debug, when I reach sscanf sscanf(param2...) it parses test1 properly and I have proper data in test1
When I reach and finish execution of scanf(param1...) it overwrites data and makes test1 data 0
Please advice.
Thanks from now
ASKER
it's IP address, %c not works but %d works... and it fits... Just when I call it second time just 2 lines below it corrupts previous data
Did you understand what I meant in my previous post ?
%d will write 4 bytes (typically), not just 1 byte as you want it to. You'll need to either read them into 4 ints rather than 4 chars, or you'll have to read and process the 4 values one by one.
%d will write 4 bytes (typically), not just 1 byte as you want it to. You'll need to either read them into 4 ints rather than 4 chars, or you'll have to read and process the 4 values one by one.
ASKER
>> read and process the 4 values one by one.
How?
reading them in 4 chars is not a case
How?
reading them in 4 chars is not a case
ASKER
>> read and process the 4 values one by one.
How?
reading them in 4 ints is not a case
How?
reading them in 4 ints is not a case
You read an int, and cast it to an unsigned char.
ASKER
>> It's your code
Heh, got me ;)
But, why not use inet_pton, which was my first suggestion ?
Heh, got me ;)
But, why not use inet_pton, which was my first suggestion ?
ASKER
>> Heh, got me ;)
:-)
>> But, why not use inet_pton, which was my first suggestion ?
I want to use something like sscanf or alternatives to that, I don't want to include a lib, I need just a function or a piece of code
:-)
>> But, why not use inet_pton, which was my first suggestion ?
I want to use something like sscanf or alternatives to that, I don't want to include a lib, I need just a function or a piece of code
ASKER
Also I should mention again that it works first time, when I re-call it just 2 lines below, it overwrites bytes above... But for first time, it works properly
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> But for first time, it works properly
That's because of endianness. Your platform is apparently little endian ... But it's dangerous code, since you're writing past the end of the buffer (3 bytes past it to be exact), thus overwriting memory that doesn' t belong to the buffer.
If you'll only run this code on a little endian system with 32bit integers, you can still use my original code (from the previous question), as long as you increase the buffer size to 7 bytes instead of 4.
That's because of endianness. Your platform is apparently little endian ... But it's dangerous code, since you're writing past the end of the buffer (3 bytes past it to be exact), thus overwriting memory that doesn' t belong to the buffer.
If you'll only run this code on a little endian system with 32bit integers, you can still use my original code (from the previous question), as long as you increase the buffer size to 7 bytes instead of 4.
ASKER
Thank you so much!
I've got to go now, but I'll be back in a few hours if there are further questions/problems.
ASKER
Code works properly, thank you so much
Either you want to read characters, and then you should use %c instead of %d, or you want to read integer values, and then you should provide enough room for an int.