Banned C standard library functions in Git source code
1–10 of 329 posts
Re: Banned C standard library functions in Git source code
#2Re: Banned C standard library functions in Git source code
#3Re: Banned C standard library functions in Git source code
#4Because of possible string buffer overflow?
Re: Banned C standard library functions in Git source code
#5I guess they care too much about portability to use "pragma GCC poison"?
Re: Banned C standard library functions in Git source code
#6Because of possible string buffer overflow?
Yes, and interestingly, because even when used correctly they "complicate audits". This is an interesting use of preprocessor macros, I'm strongly debating introducing something like this at work.
Re: Banned C standard library functions in Git source code
#7Why is strncpy insecure?
https://stackoverflow.com/questions/869883/why-is-strncpy-in...
> strncpy() doesn't require NUL termination, and is therefore susceptible to a variety of exploits.
Re: Banned C standard library functions in Git source code
#8Re: Banned C standard library functions in Git source code
#9Earlier quoted context omitted.
Yes, and interestingly, because even when used correctly they "complicate audits". This is an interesting use of preprocessor macros, I'm strongly debating introducing something like this at work.
I'm not an expert in C, but then what's the issue with strncpy() or any "n" functions? It prevents overflow AFAIK. Also what is the alternative (memcpy?) and why?
Here is an example code to demonstrate the problem:
#include
#include
int main()
{
char a[] = "01234567";
strncpy(a, "foobar", 4);
printf("%.8s\n", a);
return 0;
}
Here is the output: $ cc -std=c89 -Wall -Wextra -pedantic foo.c && ./a.out
foob4567
A C89-conforming alternative I use is a macro like this that guarantees '\0'-termination as the first thing: #define strcp(a, b, c) (a[0] = '\0', strncat(a, b, c - 1))
An example from my code: https://github.com/susam/uncap/blob/master/uncap.c#L78-L87Here is how to use it:
#include
#include
#define strcp(a, b, c) (a[0] = '\0', strncat(a, b, c - 1))
int main()
{
char a[] = "01234567";
strcp(a, "foobar", 4);
printf("%.8s\n", a);
return 0;
}
Here is the output: $ cc -std=c89 -Wall -Wextra -pedantic foo.c && ./a.out
fooRe: Banned C standard library functions in Git source code
#10That's a surprisingly small list, missing e.g. sscanf / gets / strtok / all the other "usual suspects" at least