Solved

Double sscanf confilicts, overwrites over variables

Posted on 2008-10-25
15
274 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:CSecurity
  • 8
  • 7
15 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
%d reads an integer value. You only provide room for one char. The size of an int is typically bigger than that of a char (often 4 times as big).

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.
0
 
LVL 17

Author Comment

by:CSecurity
Comment Utility
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
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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.
0
 
LVL 17

Author Comment

by:CSecurity
Comment Utility
>> read and process the 4 values one by one.

How?

reading them in 4 chars is not a case
0
 
LVL 17

Author Comment

by:CSecurity
Comment Utility
>> read and process the 4 values one by one.

How?

reading them in 4 ints is not a case
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
You read an int, and cast it to an unsigned char.
0
 
LVL 17

Author Comment

by:CSecurity
Comment Utility
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> It's your code

Heh, got me ;)

But, why not use inet_pton, which was my first suggestion ?
0
 
LVL 17

Author Comment

by:CSecurity
Comment Utility
>> 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
0
 
LVL 17

Author Comment

by:CSecurity
Comment Utility
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
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
Try something like this instead :
const char *ip_addr = "192.168.0.1";

unsigned char ip[4] = { 0 };

unsigned int tmp[4] = { 0 };

if (4 == sscanf(ip_addr, "%u.%u.%u.%u", &(tmp[0]), &(tmp[1]), &(tmp[2]), &(tmp[3]))) {

  int i = 0;

  for (i = 0; i < 4; ++i) {

    ip[i] = (unsigned char) tmp[i];

  }

}

else {

  // error parsing the IP address !!

}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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.
0
 
LVL 17

Author Closing Comment

by:CSecurity
Comment Utility
Thank you so much!
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
I've got to go now, but I'll be back in a few hours if there are further questions/problems.
0
 
LVL 17

Author Comment

by:CSecurity
Comment Utility
Code works properly, thank you so much
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now