From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f7344,2afd440a210c7fb0 X-Google-Attributes: gidf7344,public X-Google-Thread: 1164f8,df8a3dc583bf1f5f X-Google-Attributes: gid1164f8,public From: mathog@seqaxp.bio.caltech.edu Subject: Re: Compaq prefers Alpha over Merced! BUT OF COURSE, ALPHA * Date: 1998/08/29 Message-ID: <6s7hcd$sgs@gap.cco.caltech.edu>#1/1 X-Deja-AN: 385811259 References: <0095000017115526000002L062*@MHS> <6rukse$3q0$1@usenet01.srv.cis.pitt.edu> <6s17hi$2g8@post.gsfc.nasa.gov> <6s4o0o$q31@news1.panix.com> <6s6i11$jla$1@vixen.cso.uiuc.edu> <35fbd212.2154018254@news.axion.bt.co.uk> Organization: Biology Division, Caltech, Pasadena CA 91125 Reply-To: mathog@seqaxp.bio.caltech.edu Newsgroups: comp.os.vms,comp.sys.dec In article <35fbd212.2154018254@news.axion.bt.co.uk>, thomasgd@omc.bt.co.uk (Greg Thomas) writes: > >Think of OpenVMS descriptors, and have a pascal_strings.h which >contains ... > > typedef struct > { > unsigned int length; > void *string_ptr; > } pascal_string_t; That stores the length, but does not store the maximum length, so you're still into buffer overrun land for safe_strcpy. Better would be: typedef struct { unsigned int storage_length; unsigned int length; void *string_ptr; } pascal_string_t; Where storage_length is either set at malloc() time, or if strings are packed into one big memory space, shortly thereafter. So it wastes a bit of space in the data, but might end up being more efficient in other ways. For instance status=safe_strcat(safe_string1,safe_string2); can check the sizes *before* it does anything, and then use a nice efficient loop to transfer one onto the end of the other. You call it, you know you're not going to get a buffer overrun. Unfortunately, null terminated strings permeate the libraries, so that you would probably have to resort to making all such strings one character longer than they would overwise be (storage_length - 1) so that you can pop a '\0' onto the end before calling printf (or whatever). That's still nasty though - what if somebody passes you a read only safe_string? To save the extra byte your safe_string.c module would have to implement dozens, and dozens of functions. That said, it would not be at all a bad idea to do so, since null terminated strings seem to be at the root of at least half the problems in C and C based operating systems. Regards, David Mathog mathog@seqaxp.bio.caltech.edu Manager, sequence analysis facility, biology division, Caltech ************************************************************************** * RIP VMS * **************************************************************************