security - Secure C and the universities - trained for buffer overflow -


I recently finished a university course in C. That's why I lack experience.

Some universities teach safe programs for their students. Due to being in C (there is taken from)

, the string needs to be copied - as far as I know - strcpy or string.h function; How to use? Do you have some functions, which handle allocation to prevent buffer overflow? This example is presenting the example and compliance solution:

  int main (int argc, char * argv []) {/ * ... * / char prog_name [128]; Strcpy (prog_name, argv [0]); / * ... * /}  

and they are optional:

  int main (int argc, char * argv []) {/ *. .. * / char * prog_name = (four *) malloc (strlen (argv [0]) + 1); If (prog_name! = NULL) {strcpy (prog_name, argv [0]); } And {/ * could not receive memory * -} * / * ... * /}  

taken from here.

But so far as I think it's just more challenging, more code, and work, why does not any library change? Or at least why does not one provide a secure alternative library or function, which handles it properly?

Thanks for reading, wishing

Pausx function for this (almost every Is available on the system) is strdup () . strcpy () is used if you do not want to allocate new memory and want to use a buffer already, but then you better know how big a buffer is and if the string Fits in if you do not know if the string fits, then strncpy () , which copies the given number of characters, then you have to copy the amount copied to your buffers size Can be limited.

And besides, there are several sting libraries that manage string sizes in different ways.

And since you've tagged it C ++: std :: string that manages all the memory for you and does not give you these problems.


Comments