Add safety string functions ("strcpy_s", etc.)

Posted By: AndrewAMD

Add safety string functions ("strcpy_s", etc.) - 01/21/20 11:56

jcl,

Can the following string/memory functions be added to Zorro's Lite-C library?
Code
errno_t strcpy_s( char *dest, rsize_t dest_size, const char *src);
int sprintf_s(char *buffer,  size_t sizeOfBuffer,  const char *format,  ...);
errno_t strcat_s(char *strDestination, size_t numberOfElements, const char *strSource);
errno_t memcpy_s(void *dest,   size_t destSize,   const void *src,   size_t count);
Thanks,
Andrew
Posted By: jcl

Re: Add safety string functions ("strcpy_s", etc.) - 01/22/20 13:36

Yes, we can add them to stdio.h.
Posted By: AndrewAMD

Re: Add safety string functions ("strcpy_s", etc.) - 01/22/20 15:25

Thank you!
Posted By: kalmar

Re: Add safety string functions ("strcpy_s", etc.) - 01/25/20 16:32

Hi, Andrew,

Could you please explain a bit the idea behind?

Thank you!
Posted By: AndrewAMD

Re: Add safety string functions ("strcpy_s", etc.) - 01/25/20 16:43

These functions require the programmer to define the sizes of the buffer being written to.

If the function detects that the output buffer is too small, then it will, depending on the settings/implementation:
* Halt the program and clearly explain what the problem is.
* Throw an exception (not supported in Lite-C).
* Or simply return an error, and then the programmer can handle it manually without aborting the program.
* A callback function can be used in the event of any of the above.

In all cases, the bad buffer write does not occur.

Interestingly, the git project has banned all uses of strcpy(), strcat(), and sprintf() using a header:
https://github.com/git/git/blob/master/banned.h

Background: I just had to scrub one of my trading systems clean because it was crashing under mysterious circumstances. After much work, the bug disappeared for reasons unknown.

Murphy's Law: Things that can go wrong will go wrong. It's better to try to get things right the first time around.

[Linked Image]

Attached picture developers_projects.jpg
© 2024 lite-C Forums