Re: Access violation at the end of the program

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:u0i8d39s5uuq4at5j0t0hji6khprank5ph@xxxxxxxxxx
On Tue, 28 Aug 2007 15:16:43 -0000, Hunter <Igal.Hunter@xxxxxxxxx> wrote:

// Global Vars
char SystemTimeChar[] = "yyyy_mm_dd-HH_mm_ss_ccc";

void main ()
{
char logfilename[] = "EV_yymmdd_hhmmssccc.txt";
SetFileName (logfilename);

void RetrieveDateTime(char *SystemTimeChar)
{ // return date and time format: yyyy_mm_dd-HH_mm_ss_ccc
sprintf(SystemTimeChar, "%04d_%02d_%02d-%02d_%02d_%02d_%03d",
SysTime.wYear, SysTime.wMonth,\
SysTime.wDay, SysTime.wHour, SysTime.wMinute, SysTime.wSecond,
SysTime.wMilliseconds);

void SetFileName(char *filename)
{
RetrieveDateTime(SystemTimeChar);
sprintf(filename, "EV-%s.%s", SystemTimeChar, EXT);

You have a buffer overrun in SetFileName's sprintf, which is overwriting
main's return address on the stack. I'd guess you got to this point by
changing the literal you use to initialize SystemTimeChar, but you forgot
to update the literal you use to initialize logfilename. You should always
try to avoid this sort of dependency; here, you can use string
concatenation:

#define TIME_FORMAT "yyyy_mm_dd-HH_mm_ss_ccc"
char SystemTimeChar[] = TIME_FORMAT;
char logfilename[] = "EV_" TIME_FORMAT ".txt";

Now you only need to update TIME_FORMAT. Of course, your filename
extension
had better not be longer than 3 characters.

A little extra buffer size, like _MAX_PATH + 1, surely wouldn't hurt,
because there's only one of these buffers. Trying to size the buffer as
small as possible is only worthwhile if you're making hundreds or thousands
of them.


In the future, to help diagnose problems like this, look into the
compiler's /RTCs option, and to help avoid problems like this, look into
the "safe" version of sprintf and friends. Just be sure you don't ignore
the return codes for the latter. When you're using the original versions
of
sprintf, strcpy, etc, there is no room for error.

P.S. None of the line continuation characters you've sprinkled throughout
your code are necessary. Also, it's "int main()", not "void main()".

--
Doug Harrison
Visual C++ MVP


.



Relevant Pages

  • Re: Access violation at the end of the program
    ... void RetrieveDateTime ... You have a buffer overrun in SetFileName's sprintf, ... changing the literal you use to initialize SystemTimeChar, ... Of course, your filename extension ...
    (microsoft.public.vc.language)
  • [PATCH] improve OProfile on many-way systems
    ... -static void timer_ping; ... +/* Take ownership of the task struct and place it on the ... Only after two full buffer syncs ... retrieving revision 1.2 ...
    (Linux-Kernel)
  • c6713 audio project
    ... I've decided for a major project to design a digital audio effects ... buffers are used ... PING buffer first, then the PONG buffer. ... void initMcbsp; ...
    (comp.dsp)
  • Re: Delta Queue Help - Paging Mr. Kirwan
    ... printffunction in a task, do a context switch in the middle of it, ... A static buffer protected by a semaphore would also be ... You mentioned sprintf(). ... then the operating system 'preempts' you and forces control ...
    (comp.arch.embedded)
  • Re: Im a C++ programmer, and Relfs X.CPP is good.
    ... void write_path(char* hm_path, char hm_buf); ... void list_contents(int argc, char** argv); ... Makes sure the user has specified an alpha buffer. ...
    (comp.lang.lisp)