473,725 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does wrong format specifier leads to memory corruption?

Hi,

We are having memory corruption in our application somewhere, unable to find
out.
one part of code we found that we are specifying wrong format specifier.

Could anyone let me know if the following code may cause memory corruption.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100]={0};
strcpy(str,"tem porary");
char str2[100]={0};
sprintf(str2,"% S",str); // this line should have had %s instead of %S
return 0;
};

TFH
ishekara


Nov 14 '05 #1
4 2436
"indushekar a" <in*********@gm ail.com> wrote:
Could anyone let me know if the following code may cause memory corruption. sprintf(str2,"% S",str); // this line should have had %s instead of %S


Since using an invalid conversion specifier causes undefined behaviour,
in theory this could cause memory corruption. I'd be rather surprised if
it did, though. I'd expect it either to work as if you'd written "%s",
to write a literal "%S" to the string, to use "%S" as an implementation-
specific specifier, perhaps for a special kind of string (capitalised?
Who knows), or maybe to fail in a relatively innocuous way, possibly by
writing nothing at all.
Writing to unrelated memory, or crashing past the end of the array, or
something similar, is a theoretical, but IMO unlikely possibility. Your
error is probably elsewhere.

Richard
Nov 14 '05 #2
On Wed, 22 Jun 2005 15:07:28 +0530, "indushekar a"
<in*********@gm ail.com> wrote:
Hi,

We are having memory corruption in our application somewhere, unable to find
out.
one part of code we found that we are specifying wrong format specifier.

Could anyone let me know if the following code may cause memory corruption.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100]={0};
strcpy(str,"te mporary");
char str2[100]={0};
sprintf(str2," %S",str); // this line should have had %s instead of %S
return 0;
};

Not likely. Does the actual code you've posted cause memory
corruption? My first guess is that the compiler is treating %S as %s,
and that the real code is using an unterminated str, or at least
strlen(str) > sizeof str2 - 1.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 15 '05 #3
Alan Balmer wrote:
<in*********@gm ail.com> wrote:

.... snip ...
sprintf(str2,"% S",str); // this line should have had %s instead of %S
return 0;
};


Not likely. Does the actual code you've posted cause memory
corruption? My first guess is that the compiler is treating %S as
%s, and that the real code is using an unterminated str, or at
least strlen(str) > sizeof str2 - 1.


The compiler is probably not thinking about it at all, but just
passing it onwards to the library routines to decode. The OP
should be able to track its actions completely with a debugger, and
compare with the actions with the correct specifier.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Nov 15 '05 #4
On Wed, 22 Jun 2005 18:17:07 GMT, CBFalconer <cb********@yah oo.com>
wrote:
Alan Balmer wrote:
<in*********@gm ail.com> wrote:
... snip ...
sprintf(str2,"% S",str); // this line should have had %s instead of %S
return 0;
};


Not likely. Does the actual code you've posted cause memory
corruption? My first guess is that the compiler is treating %S as
%s, and that the real code is using an unterminated str, or at
least strlen(str) > sizeof str2 - 1.


The compiler is probably not thinking about it at all, but just
passing it onwards to the library routines to decode.


Quite right. Sloppy writing on my part. s /compiler/runtime/.
The OP
should be able to track its actions completely with a debugger, and
compare with the actions with the correct specifier.


Or just correct the specifier and see if it stills clobbers the
memory.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2046
by: Generic Usenet Account | last post by:
While I have a very good feel for how inlining works, I fail to see how in the world inlining can work if the inlined function is not described "in place" in a header file, but rather defined in a separate source file (using the inline keyword), which gets linked in? Does inling work at all in such cases? If it does, can someone kindly explain how the compilers handle that? If it does not, is that documented somewhere? Thanks, Gus
8
3412
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
6
5759
by: alternativa | last post by:
Hi, I have problem with the following function - it was intended to ask a user for a 4-digits number: double ask_for_number (void) { char *notint; char s2; double entered_number;
9
2155
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
130
6588
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places in the database show up with *15* decimal places and are ever so slightly off (in the example in the subject line, by about 2E-15). I'm not doing any operations on this column. It's just running a stored procedure which performs a pretty basic...
15
2686
by: E-Dot | last post by:
I am trying to write a program which asks the user to enter a number in the interval , the program then gives the natural logarithm of that number, using the series for log(x+1)... Here is what I have so far and can't figure out what i'm doing wrong. any help would be greatly appreciated, thanks guys... #include <stdio.h> #include <math.h>
8
2881
by: Andrew | last post by:
I am currently designing a database, using Access 2003 running on Windows Vista Premium Home. The database file format is Access 2002 / 2003 (mdb). The database is stored on a 1Gb USB memory stick so that I can work on it anywhere without having to keep updating it on various machines. The disk has never had any other errors, all other files are fine, it has plenty of space and checkdisk reports no errors. The database is split...
14
3365
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought I'll make the inactive forms invisible but this is creating a memory corruption problem when user close the form2 or form3 and not the formMain. My main form has a Next button which makes the main form invisible and starts a new form which I'll...
21
1499
by: google | last post by:
Hello, in embedded programming, different kinds of memory exist, e.g. RAM and ROM (Flash memory). For a class containing variables and constant values one might want to put the variables in RAM and the constants in ROM, which means that the object is split into different parts in memory. Besides from the fact that the current compiler does not support such feature: does the C++ standard require an that the objects data
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.