News for hackers. Stuff that matters.
Re:Making a 3D modeler in C in a week by kragen on Thursday May 02, 2024 @11:23PM (#40243717)
in c and c++ this is not just normal but almost unavoidable. if your function takes a t* and someone casts a random int to a t* and pass it in, it is gonna segfault. no possible way to validate it, though in theory you could open /proc/self/maps and iterate through it to catch the segfault cases, or install a segfault handler
Re:Making a 3D modeler in C in a week by lelanthran on Friday May 03, 2024 @12:44AM (#40244128)
> if your function takes a t* and someone casts a random int to a t* and pass it in, it is gonna segfault.

True, that is not something a function ought to defend against.

However, the complaint was not about that, though, it was about not checking if the dataSize parameter is NULL.

I don't really have a problem with functions that segfault when given NULL pointer parameters as long as this is clearly documented!

Sometime, however, you just need to be familiar with enough projects in C that common-sense gets built. In the specified example:

     unsigned char *LoadFileData(const char *fileName, int *dataSize);
I expected that a function that returns an array needs to tell the caller how large that array is. This specific function is not one I would complain about.

These things are usually documented in the headers. In this case it says:

      // Load file data as byte array (read)
So, yeah, it's pretty clear to me that the number of bytes has to be returned somewhere, and there's only one parameter called `dataSize`, so this isn't something I consider to be a valid complaint.

[EDIT: Escaped pointers, and added last paragraph]

Re:Making a 3D modeler in C in a week by defrost on Friday May 03, 2024 @12:56AM (#40244193)
> unsigned char

WARNING unpaired | unescaped *'s !! :-)

grrr, markup

Re:Making a 3D modeler in C in a week by lelanthran on Friday May 03, 2024 @01:03AM (#40244238)
Thanks, fixed.
Re:Making a 3D modeler in C in a week by naasking on Friday May 03, 2024 @09:24AM (#40247403)
> Sometime, however, you just need to be familiar with enough projects in C that common-sense gets built.

And if every C project takes this same philosophy to poor documentation, how exactly is a new person learning C supposed to build that common sense? Brushing off poor documentation with "you just need more experience" is not a valid response. The documentation for that function could be much more clearer without being much longer.

Re:Making a 3D modeler in C in a week by lelanthran on Friday May 03, 2024 @09:49AM (#40247641)
My point is not that it was good or acceptable, my point is that, while not perfect, it isn't bad enough to warrant the moniker of `insane`.
Re:Making a 3D modeler in C in a week by kragen on Friday May 03, 2024 @01:33PM (#40250164)
yeah. unlike an arbitrary garbage pointer, a null pointer is something the callee could detect. but on linux or windows the operating system will reliably detect it for you, and in a way that makes it a lot easier to debug: it segfaults, making it obvious that there's a bug, and your debugger will show you the stack trace leading up to the LoadFileData call with the null pointer parameter. and, as you point out in your other comment, in the very next line after your call to LoadFileData, you need the data size, and if you passed in a statically null pointer, you don't have it, so you realize you're going to need to pass in a pointer to an actual int

so, while the documentation could and should be more explicit than the single telegraphic line you quote, the function's behavior is fine, and detecting and trying to handle the null pointer would make its behavior worse and harder to debug (except on like an arduino or ms-dos or something)

not every c api description needs to be an introductory tutorial for c

Re:Making a 3D modeler in C in a week by pjmlp on Saturday May 04, 2024 @10:01AM (#40257748)
Even that isn't fullproof.

Microsoft has deprecated all pointer validation functions on Windows, because there were plenty of corner cases regarding pointer validation.

Re:Making a 3D modeler in C in a week by kragen on Saturday May 04, 2024 @04:45PM (#40260153)
aha, thanks. yeah obviously /proc/self/maps won't help much there