Thanks. Right. I was thinking of several such possibilities.

There are also "safe" versions of many of the char[] functions including
sprintf, so several of these would have to be written. sprintf would be a
little trickier than the below.

I was just surprised that IBM did not provide "safe" versions given that (in
my informal survey of malware issues) the number one cause of security
problems in software is "buffer overrun."

Charles

-----Original Message-----
From: IBM Mainframe Discussion List [mailto:[email protected]] On Behalf
Of McKown, John
Sent: Thursday, December 03, 2009 7:54 AM
To: [email protected]
Subject: Re: Is there a good mailing list or forum for mainframe C/C++
specifically?

> -----Original Message-----
> From: IBM Mainframe Discussion List 
> [mailto:[email protected]] On Behalf Of Charles Mills
> Sent: Thursday, December 03, 2009 9:08 AM
> To: [email protected]
> Subject: Re: Is there a good mailing list or forum for 
> mainframe C/C++ specifically?
> 
> I'm new to C++ so there are going to be a LOT of them. <g>
> 
> The question of the moment is "is there no 'safe' string copy library
> routine such as strcpy_s? I don't see it in the doc."
> 
> Charles

Out of curiousity, I looked at the MSDN article for strcpy_s. Basically, it
copies the bytes from one area to another, but only if the entire source
will fit in the destination area (the size of which is passed in the
strcpy_s parm list). If there is any "problem", then the destination is not
modified at all. I don't see any such in z/OS C, but you could "roll your
own". I'd likely use memcpy(). Something like:

#define _X_OPEN
#include <string.h>
#include <errno.h>
int strcpy_s(char *dest, size_t elements, const char *source) {
   size_t src_len;
   if (dest == NULL) return EINVAL;
   if (source == NULL) return EINVAL;
   if (elements == 0) return ERANGE;
   src_len=strlen(source)+1; /* add 1 to include \0 terminator */
   if (src_len > elements) return ERANGE;
   memcpy(dest,source,src_len);
   return 0;
}

--
John McKown 

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: GET IBM-MAIN INFO
Search the archives at http://bama.ua.edu/archives/ibm-main.html

Reply via email to