<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Is this C language statement a problem? in Operating System - OpenVMS</title>
    <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177419#M42350</link>
    <description>That's correct, your sprintf is illegal, unless there is only 1 byte and a null at currStlDate[2].&lt;BR /&gt;&lt;BR /&gt;The C99 Standard has the "restrict" keyword for the target buffer in sprintf.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Isn't this what memory overlapping is?&lt;BR /&gt;&lt;BR /&gt;Exactly.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;If copying takes place between objects that overlap, the behavior is undefined.&lt;BR /&gt;&lt;BR /&gt;C99 is even clearer:&lt;BR /&gt;int sprintf(restrict char *s, const char *format, ...);&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Joseph: As far I see standards say nothing about the target of sprintf&lt;BR /&gt;&lt;BR /&gt;The Standard is quite clear.</description>
    <pubDate>Sat, 30 May 2009 15:06:04 GMT</pubDate>
    <dc:creator>Dennis Handly</dc:creator>
    <dc:date>2009-05-30T15:06:04Z</dc:date>
    <item>
      <title>Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177412#M42343</link>
      <description>Hi Guys,&lt;BR /&gt;&lt;BR /&gt;I have a C snippet that someone here at work says could be problematic. That the copying of memory locations should not overlap.&lt;BR /&gt;&lt;BR /&gt;I've never had a problem with this style on DEC C, but maybe I've been missing something.&lt;BR /&gt;&lt;BR /&gt;Can anyone offer a reliable opinion on whether it is 'dangerous' code?&lt;BR /&gt;&lt;BR /&gt;The snippet is:&lt;BR /&gt;&lt;BR /&gt;  char      currStlDate[8+1];&lt;BR /&gt;  ...&lt;BR /&gt;  memset(currStlDate, 0, sizeof (currStlDate));&lt;BR /&gt;  ...&lt;BR /&gt;  /* Here currStlDate is "20090526" */&lt;BR /&gt;  sprintf(currStlDate, "%s", &amp;amp;currStlDate[2]);&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 26 May 2009 06:33:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177412#M42343</guid>
      <dc:creator>Jimson_1</dc:creator>
      <dc:date>2009-05-26T06:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177413#M42344</link>
      <description>I can't see any (overlapping) memory copy in Your snippet.&lt;BR /&gt;???&lt;BR /&gt;&lt;BR /&gt;strcpy(dst,src): must not overlap &lt;BR /&gt;memmove(dst,src,size): may overlap&lt;BR /&gt;</description>
      <pubDate>Tue, 26 May 2009 07:52:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177413#M42344</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-05-26T07:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177414#M42345</link>
      <description>So the above snippet is not a problem?&lt;BR /&gt;&lt;BR /&gt;Both source and dest pointers in the sprintf statement point to memory locations where dest will overwrite source.&lt;BR /&gt;&lt;BR /&gt;Isn't this what memory overlapping is?</description>
      <pubDate>Tue, 26 May 2009 09:46:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177414#M42345</guid>
      <dc:creator>Jimson_1</dc:creator>
      <dc:date>2009-05-26T09:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177415#M42346</link>
      <description>Now I see the side-effect of sprintf is what you called overlapping 'copy'.&lt;BR /&gt;As far I see standards say nothing about the target of sprintf (but the question may be better placed in comp.lang.c !).&lt;BR /&gt;In practice I see nothing against doing it.</description>
      <pubDate>Tue, 26 May 2009 10:23:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177415#M42346</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-05-26T10:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177416#M42347</link>
      <description>imho this is poor coding, and dangerous from a maintenance perspective.&lt;BR /&gt;&lt;BR /&gt;- the char line should have a comment:&lt;BR /&gt;   // zero terminated ccyymmdd or yymmdd format.&lt;BR /&gt;&lt;BR /&gt;- the memset suggests that the coder wasn't sure when or how the date string will be used. It is likely to be entirely redundant, or should be replaced by: currStlDate[0] = 0&lt;BR /&gt;&lt;BR /&gt;- the sprintf suggests that the coder was too  lazy to make a clear 'ccyymmdd' versus 'yymmdd' position. It seems unacceptable to me to have a variable sometimes hold one flavor, sometimes the other. It should probably be replaced by: &lt;BR /&gt;char *currStlDate_yymmdd = &amp;amp;currStlDate[2];&lt;BR /&gt;&lt;BR /&gt;fwiw&lt;BR /&gt;Hein.</description>
      <pubDate>Tue, 26 May 2009 10:34:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177416#M42347</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2009-05-26T10:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177417#M42348</link>
      <description>Hein's correct comment on the date semantics aside,&lt;BR /&gt;just curious I made a test on really overlapping (not just substring shuffling)  sprintf :&lt;BR /&gt;/*----------------------------------------*/&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt; int main(){&lt;BR /&gt;  char test[50];&lt;BR /&gt; &lt;BR /&gt;  strcpy(test,"abcdefghijklmnopqrstuvwxyz1234567890");&lt;BR /&gt;&lt;BR /&gt;  sprintf(test,"%s",test);&lt;BR /&gt;  printf("%s\n",test);&lt;BR /&gt;  sprintf(test,"   %s",&amp;amp;test[2]);&lt;BR /&gt;  printf("%s\n",test);&lt;BR /&gt; }&lt;BR /&gt;/*------------------------------------------*/&lt;BR /&gt;&lt;BR /&gt;One would expect the first 3 or 4 characters blanked, but the rest of the string simply copied.&lt;BR /&gt;On VMS/DECC: yes, it is so:&lt;BR /&gt;run test_sprintf&lt;BR /&gt;abcdefghijklmnopqrstuvwxyz1234567890&lt;BR /&gt;    defghijklmnopqrstuvwxyz1234567890&lt;BR /&gt;&lt;BR /&gt;On RH Linux/GCC: what a surprise:&lt;BR /&gt;./a.out&lt;BR /&gt;abcdefghijklmnopqrstuvwxyz1234567890&lt;BR /&gt;    ddfghhjkllnopprsttvwxxz1224566890&lt;BR /&gt;&lt;BR /&gt;&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Tue, 26 May 2009 10:43:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177417#M42348</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-05-26T10:43:17Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177418#M42349</link>
      <description>Ok I've answered my own question.&lt;BR /&gt;&lt;BR /&gt;The documentation states:&lt;BR /&gt;&lt;BR /&gt;int sprintf(char *s, const char *format, ...);&lt;BR /&gt;Equivalent to the fprintf function except that the argument s specifies&lt;BR /&gt;an array, rather than a stream, into which the generated output will be&lt;BR /&gt;written. A null character is written at the end of the characters written. If&lt;BR /&gt;copying takes place between objects that overlap, the behavior is undefined.&lt;BR /&gt;The sprintf function returns the number of characters written into the&lt;BR /&gt;array, not counting the terminating null character.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks for your answers anyway.&lt;BR /&gt;&lt;BR /&gt;James P.&lt;BR /&gt;</description>
      <pubDate>Tue, 26 May 2009 10:44:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177418#M42349</guid>
      <dc:creator>Jimson_1</dc:creator>
      <dc:date>2009-05-26T10:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177419#M42350</link>
      <description>That's correct, your sprintf is illegal, unless there is only 1 byte and a null at currStlDate[2].&lt;BR /&gt;&lt;BR /&gt;The C99 Standard has the "restrict" keyword for the target buffer in sprintf.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Isn't this what memory overlapping is?&lt;BR /&gt;&lt;BR /&gt;Exactly.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;If copying takes place between objects that overlap, the behavior is undefined.&lt;BR /&gt;&lt;BR /&gt;C99 is even clearer:&lt;BR /&gt;int sprintf(restrict char *s, const char *format, ...);&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Joseph: As far I see standards say nothing about the target of sprintf&lt;BR /&gt;&lt;BR /&gt;The Standard is quite clear.</description>
      <pubDate>Sat, 30 May 2009 15:06:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177419#M42350</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2009-05-30T15:06:04Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177420#M42351</link>
      <description>Ok that great, thanks all for you help.&lt;BR /&gt;&lt;BR /&gt;James P.</description>
      <pubDate>Thu, 04 Jun 2009 06:12:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177420#M42351</guid>
      <dc:creator>Jimson_1</dc:creator>
      <dc:date>2009-06-04T06:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: Is this C language statement a problem?</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177421#M42352</link>
      <description>Closed</description>
      <pubDate>Thu, 04 Jun 2009 06:12:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/is-this-c-language-statement-a-problem/m-p/5177421#M42352</guid>
      <dc:creator>Jimson_1</dc:creator>
      <dc:date>2009-06-04T06:12:41Z</dc:date>
    </item>
  </channel>
</rss>

