From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fdb0e,cdca3dc8d6167cd4 X-Google-Attributes: gidfdb0e,public X-Google-Thread: 1164f8,cdca3dc8d6167cd4 X-Google-Attributes: gid1164f8,public X-Google-Thread: f7344,a808e23c428d1184 X-Google-Attributes: gidf7344,public X-Google-ArrivalTime: 2001-12-22 11:42:05 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.shore.net!Ives.egh.com!JOHN Newsgroups: comp.os.vms,alt.sys.pdp10,comp.sys.dec From: John Santos Subject: Re: Congratulations for the festive season In-Reply-To: Message-ID: <1011222142241.59837C-100000@Ives.egh.com> References: <1011221190540.59837B-100000@Ives.egh.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Date: Sat, 22 Dec 2001 14:42:52 -0500 NNTP-Posting-Host: 209.192.204.112 X-Complaints-To: abuse@shore.net X-Trace: news.shore.net 1009050114 209.192.204.112 (Sat, 22 Dec 2001 14:41:54 EST) NNTP-Posting-Date: Sat, 22 Dec 2001 14:41:54 EST Organization: Shore.Net, a PRIMUS Company (info@shore.net) Xref: archiver1.google.com comp.os.vms:56844 alt.sys.pdp10:8647 comp.sys.dec:10191 On Sat, 22 Dec 2001, Paul Sture wrote: > In article <1011221190540.59837B-100000@Ives.egh.com>, John Santos wrote: > > > [snip] > > > This turns out not to be the case. Most VMS languages use counted > > strings and the OTS routines that do string copies that check the > > counts when copying to fixed-length buffers, and truncate and/or > > warn you (but never, ever write past the end of the destination.) > > This philosophy has been present since day one - ever notice the very > > non-RISCy MOVC5 instruction in the VAX instruction set? > > > > Sure this hurts performance sometimes: you have to store, maintain, > > check and pass around all those pesky counts, and you can't do the > > cheap trick of copy bytes until you hit a in a tight loop. > > Instead you have to compare source and destination sizes, set your > > loop counter to the minimum, and copy. Sometimes you don't get > > to stop early if the source string is short, since you may need to > > fill the destination. On the other hand, is not a special > > character in strings. (In-band flow control is generally a bad > > idea.) > > > > But what's a few nano-seconds if it saves you hours of > > crash/reboot/recovery time and debugging? Especially when the fix > > is to manually insert code that does the same bounds checking, in > > a less efficient manner? > > > Here's a point though. Those extra nano seconds mean that your > subroutines (APIs, whatever), don't have to scan up a string themselves > to determine its length. That's why I refered to the fix as being "less efficient". Worst case, the broken routine needs to count the characters in the input string, and then use min(length,dest-buffer-size) in its copy loop. Often you can code this more efficiently by checking for the in the source string and the end of the output buffer as two different loop termination conditions, but you have to be careful to do them in the right order, this is still slower (more stuff to check in the loop) because you don't know the source string length up front, and you have to handle the case where the source string is 1 byte longer than the buffer, but that extra byte is the terminating and thus the input string is okay. I guess I'm trying to derail the common argument that "sure programming in C is dangerous, but it is more efficient in the safe cases, and it is the programmer's job to explicitly code in the checks in the unsafe cases." In this day of optimizing compilers and GHz processors, isn't it smarter to always do the checks and let the machine decide when it is redundant? Let the programmer get on with the program, and not write 3 lines of code when one would do it in a language that actually supports strings. > For any program dealing with fixed length records, determining the record > length during its initialization phase means it's then available for any > routine to use, for the program's execution lifetime. > > I'd suggest that the CPU consumed on a Windows machine continually scanning > every buffer passed its APIs is probably many times greater than that used > by a mechanism of passing the length (which you calculated once at the > beginning anyway). > > Variable length records are only slightly harder - if you are constructing a > variable string you likely know how long it is already, as part of the > construction process. Yes. > > Most (all?) exceptions to the above are in C programs that use > > -terminated strings, and most of those were imported from > > Unix, so I don't understand why the Unix users would be laughing. > > I think they would be too busy fixing their own systems to be > > worrying about others. > > > > Or maybe if the implementors of most of the public-domain software > > (TCP/IP, etc., etc.) had grown up on VMS instead of Unix, they > > would have been dismayed on discovering the lack of buffer overflow > > protection in the usual C libraries and would have insisted on > > (or implemented themselves) a standard set of counted string > > functions that everyone would use, and would have soon acquired > > direct support in ANSI C (for counted string literals and constants) > > and buffer overflow attacks on Unix would be a thing of the past. > > Good point. > ___ > Paul Sture > Switzerland -- John Santos Evans Griffiths & Hart, Inc. 781-861-0670 ext 539