Live data from Hacker News

Banned C standard library functions in Git source code

github.com

11–20 of 329 posts

Re: Banned C standard library functions in Git source code

#12
post #6

Earlier 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?

It doesn’t ensure that there’s a null terminator in the resultant string. This may potentially lead to a later overrun when the copied string is read back.

(See also https://linux.die.net/man/3/strncpy )

Re: Banned C standard library functions in Git source code

#13
Windows programmers are familiar with StrSafe.h, https://en.wikipedia.org/wiki/Strsafe.h and https://github.com/dotnet/coreclr/blob/master/src/pal/inc/st... which go a lot further.

Also found https://github.com/mubix/netview/blob/master/banned.h on Github with a better list.

Re: Banned C standard library functions in Git source code

#14
post #5
post #2

I guess they care too much about portability to use "pragma GCC poison"?

Wanting code to compile on something other than gcc is not "caring too much", it's being responsible

Responsible? What an hyperbolic statement. Could you enlighten me on what would be the significant negative consequences? I hope that your mental equation take into account the missed usefulness of GCC only attributes, flags and extensions by being clang compatible.

Re: Banned C standard library functions in Git source code

#15
post #6

Earlier 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?

The issue is that strncpy doesn't guarantee that the destination string is null-terminated after a copy, particularly in the case where n is shorter than the original string, so you could run into overflows if you then ran strlen() or similar against it.

Here's a good explanation from Raymond Chen: https://devblogs.microsoft.com/oldnewthing/?p=36773

Re: Banned C standard library functions in Git source code

#16
post #6

Earlier 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?

[deleted]

Re: Banned C standard library functions in Git source code

#17
post #6

Earlier 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?

strncpy have the danger of not putting a null character at the end of the destination , if source is longer than num.

> Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).

Re: Banned C standard library functions in Git source code

#18
post #5

Earlier quoted context omitted.

Wanting code to compile on something other than gcc is not "caring too much", it's being responsible

Responsible? What an hyperbolic statement. Could you enlighten me on what would be the significant negative consequences? I hope that your mental equation take into account the missed usefulness of GCC only attributes, flags and extensions by being clang compatible.

I don't know what to tell you. Do you think all compilers should implement all GCC proprietary extensions, basically creating a parallel C standard?

And that's not saying that GCC extensions are not useful, but they should not be considered the norm

Re: Banned C standard library functions in Git source code

#19
post #6

Earlier 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?

[deleted]

Re: Banned C standard library functions in Git source code

#20
post #18

Earlier quoted context omitted.

Responsible? What an hyperbolic statement. Could you enlighten me on what would be the significant negative consequences? I hope that your mental equation take into account the missed usefulness of GCC only attributes, flags and extensions by being clang compatible.

I don't know what to tell you. Do you think all compilers should implement all GCC proprietary extensions, basically creating a parallel C standard? And that's not saying that GCC extensions are not useful, but they should not be considered the norm

Agreed. But I wonder if we already have a sort of parallel C standard already with POSIX (of course the parts about C, not about OS facilities).
Post reply on HN