Re: const char * to char[]

From: Richard Bos (rlb_at_hoekstra-uitgeverij.nl)
Date: 11/19/03


Date: Wed, 19 Nov 2003 14:06:19 GMT

greywolf@the-junkyard.net (Charles Banas) wrote:

> "Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message news:<bpdcht$s0$1@news.freedom2surf.net>...
> > and I attempt to copy my AppName to NOTIFYICONDATA.szTip by:
> > NOTIFYICONDATA TrayIcon; // set structure
> > strncpy(TrayIcon.szTip, mWCS->AppName, 64); // tooltip text to display

Be careful. You happen to know that your string fits now, but consider
what would happen if AppName were exactly 64 characters long, not
including the terminating null character.

> call me naiive, but isn't it possible that the memory beyond the
> string AppName points to could be code memory?

What has that got to do with it?

> strncpy(TrayIcon.szTip, mWCS->AppName, 22);

You are confused about the behaviour of strncpy(), I suspect. It does
_not_ copy exactly N bytes - if it did, you might as well use memcpy().
What it does is copy up to either the end of the string, or N bytes,
whichever comes first. And then, superfluously, pads the rest with null
characters.

Richard



Relevant Pages

  • Re: string manipulation experts...
    ... The app name isn't a problem it is always a static, fixed length string. ... A couple of questions concering the AppName. ... determine the app name (n characters being the same length as the list ...
    (microsoft.public.scripting.vbscript)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... put them in stupid places. ... Programming is difficult (as you must surely appreciate, ... > strings will be in the range 1...1000 characters. ... impose an artificially small limit on string length." ...
    (comp.programming)
  • Re: Byte Array to String
    ... retrieved text will mismatch the original characters. ... encoding the characters. ... Dim strFileData as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: A note on personal corruption as a result of using C
    ... impossible to write effective string validation routines by definition ... (Note that a string literal may contain embedded null characters; ... without resorting to abusive language. ... In practice, programmers typically use "struct" ...
    (comp.programming)