All functions, that uses cf_ReadBytes(), checks return size, but don't handle potential exceptions. That leads to segfaults on reading of damaged files (i.e. broken savegames).
Before this change, cf_OpenLibrary() did something along the lines of
this:
char id[4];
fread(id, 4, 1, fp);
strncmp(id, "HOG2", 4);
If fread() finishes successfully, then that code is fine. However,
fread() might encounter an error or bump into the end of a file. In
those scenarios, the value of id will not necessarily be initialized
[1]. In other words, when fread() fails, strncmp() might operate on
uninitialized memory.
This change makes sure that the value of id only gets used if fread()
succeeds. Additionally, this change fixes a GCC warning about ignoring
fread()’s return value.
[1]: <https://en.cppreference.com/w/cpp/io/c/fread>
Before this change, cf_OpenLibrary() was inconsistent. Sometimes, it
would refer to the size of a HOG2 id using the literal 4. Other times it
would refer to the size of a HOG2 id using strlen(HOG_TAG_STR). There
was a good reason for this. Some compilers allow you to do this:
char id[strlen(HOG_TAG_STR)];
Other compilers throw an error if you try to do that [1].
This commit makes cf_OpenLibrary() more consistent. It makes it so that
cf_OpenLibrary() always uses the same constant expression when referring
to HOG_TAG_STR’s length.
[1]: <https://devblogs.microsoft.com/oldnewthing/20221114-00/?p=107393>
Join the license header with historical comments using a separator so IDEs can correctly parse the initial header.
Also use .gitattributes to ensure all files are LF.
* Begin by marking functions and variables as static when needed.
* More work.
* More work.
* More pokes.
* More work.
* More work.
* Initial work on the netgames.
* Revert changes to the license header on source files.
* clutter.cpp poke.
* One final poke.
* Move some declarations to headers:
Move paged_in_count and paged_in_num to gamesequence.h
Move DoneLightInstance and StartLightInstance to polymodel.h
* Look over the AI script/plug-ins.
* Going over the changes one last time.
* Fix rebase errors.
* More migration from bare statics to static inlines.
Remove unused code related to old HogUtils executable, now superseded by HogMaker.
Stricter checks for hog header and hog file entry (check fread() sizes).
The vast majority of this is fixing up `char *` that should be `const char *`
but a handful of other fixes, like potential buffer overflows that GCC
noticed, etc, were applied as well.
This removes `-Wno-write-strings` from CMakeLists.txt, as it is no longer
necessary, as there is no longer a flood of compiler warning spam when
building.
This does not fix all compiler warnings; there are still a handful, and they
are legitimate, but they can be dealt with in a future commit.