473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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,"temporary");
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 2409
"indushekara" <in*********@gmail.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, "indushekara"
<in*********@gmail.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,"temporary");
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*********@gmail.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.announce.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********@yahoo.com>
wrote:
Alan Balmer wrote:
<in*********@gmail.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
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...
8
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 ? ...
6
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
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...
130
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...
15
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...
8
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...
14
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...
21
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.