View Single Post
Old 2012-05-30, 14:33   #6
kjaget's Avatar
Jun 2005

3×43 Posts

Originally Posted by Dubslow View Post
Try this on for size:
bill@Gravemind:~/CUDALucas/test∰∂ cat parse.c

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>

#ifdef _MSC_VER
#define strncasecmp _strnicmp

void strcopy(char* dest, char* src, size_t n) 
#ifdef _MSC_VER
	strcpy_s(dest, n, src);
	strncpy(dest, src, n);

int isprime(unsigned int n) //...etc
Turns out the Linux "safe" equivalent is strncpy, much like strncasecmp. It's too bad Microsoft can't just use the same damn function. (Note the different ordering of arguments, as well as the name.)

Where it gave warnings, replace those "strcpy" calls with "strcopy", and add a third argument "MAX_LINE_LENGTH+1". Alternately, just DL the attached modified version
strncpy() isn't a safe version of strcpy(). It doesn't report errors and will not nul-terminate a string if it hits the size limit. That means it itself won't cause buffer overruns, but any subsequent use of the results of likely will if your source string was too long.

Last fiddled with by kjaget on 2012-05-30 at 14:34
kjaget is offline   Reply With Quote