Follow-up Comment #12, bug #68812 (group groff):

[comment #6 comment #6:]
> [comment #2 comment #2:]
>> Deri raised this class of issue in comment 15 to bug #68687.
> 
> Well, looking at the function printer::load_font(), that function does
> contain a few obvious potential and/or theoretical problems - but the problem
> is *not* that using the data type "int" for array indices is wrong in any
> way.  Instead, most, but not all, of the potential problems in that function
> are related to a lack of boundary and overflow checking, for example:
> 
> * The sequence "assert(n >= 0); if (n < 0) ..." is nonsensical.

The latter is reachable code if you build with `-DNDEBUG`.
 
> Instead, we should make sure that assert(3) is always honoured and never
> ignored.

This is a proposal to reform the C/C++ language.  Raise it with WG14 and/or
WG21.  This is not a windmill I personally intend to tilt at with _groff_.

> Blindly returning when a violation of an invariant is detected is extremely
> dangerous and foolish.

It certainly is, if the code doesn't take other measures.  While _groff_'s
code is far from perfect, in large part because it often fails to document its
invariants in the first place, where I add assertions or encounter existing
ones, I try to do one of two things when `NDEBUG` is defined:

1.  Fatally error out if the program is in an recoverable state due to the
invariant violation.

2.  Otherwise--and this is often the case when is already designed to return
some kind of failure indicator--return that indicator.

> When code already knows that its internal state is corrupted, it must never
> be allowed to continue.

I agree, but that's not _always_ the case with a constraint violation.  At
least not the way _groff_ is written.  But I've seen other programs that
distinguish between "impossible" situations (startling to the developers, but
recoverable) and "panic" situations (all bets are off, the program must abort
immediately).

> So instead of "return", the code needs to call abort(3) - but only if we can
> guarantee that no program using the library ever installs a SIGABRT handler.
> Otherwise, exit(3) must replace the "return".

No program _groff_ distributes installs a SIGABRT handler.

> If calling exit(3) is not acceptable in that library - in many libraries, it
> isn't, but then calling assert(3) is not acceptable either - then the
> function printer::load_font() must be redesigned to report failure, for
> example by returning 0 for success or -1 for failure, like many libc
> functions do.

_groff_ doesn't supply "resuable" libraries.  Its libraries are all static and
exist simply to prevent code duplication among executables.  I personally
_prefer_ not to _exit_(3) from library code if I can help it, but in _groff_ I
have to admit that there's not much reason to maintain such a discipline.  In
this project, even the library code is application code.
 
> * There are at least two places where something is added to the input value n
> without checking for overflow.

I'll have a look at that.

> * The operator "new" is called on the font type without sanity checking for
> unreasonably large values of the upper bound.

I suspect we should keep the _grout_ we read from going hog wild with mounting
position values as the formatter does when handling the `fp` request.

> * The old nfonts value is doubled without checking for overflow.

Good point.  I just blundered into a similar land mine of my own making in
_grotty_.

> * I'm not claiming completeness of my audit of the function.

Not necessary--I appreciate even a partial audit.  :)

> If the function is used reasonably by the rest of the code, these potential
> bugs are unlikely to matter.

That's an interesting question.  This is a "grout" interpreter, so in
principle we're reading potentially hostile input files.  In practice, the
only thing that produces "trout" or "grout" is a _troff_ program, all of which
try to be well-behaved in what they generate, and virtually no one archives or
retains files in this format.

However, it's a hostile world, so giving the _libdriver_ reader of "grout" and
"trout" input files a sterner attitude toward its input is likely wise.

> It may or may not make sense to fix some or all of them, depending on the
> bigger picture.  But that requires careful analysis of the actual bugs,

I expect it to be straightforward to hand-craft "grout" that will make this
function behave undesirably.  We'll see.

> not some blanket and bogus conclusion like "don't use 'int' for array
> indices".

As my discussion with Dave illustrated, my grievance is more with the crudity
of the C/C++ type system, and how that crudity frustrates the robust
programming of _groff_.  

> That careful analysis of what the bugs actually are *cannot* be automated,
> and potential fixes cannot be automated either.

[https://lists.gnu.org/archive/html/groff/2026-10/msg00050.html They _can_ be,
but not well.]

Hey, did ya hear?  The Riemann Hypothesis, long suspected to be true by the
great majority of mathematicians, is confirmed, thanks to an LLM.  Only 17
million lines of "Lean" to do it.

Supposedly there are two independent verifications of the, uh, proof.

Verification is, of course, not the same thing as understanding.

If I find any provokable issues with the code I'll spawn off new tickets for
them as necessary.


    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?68812>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/

Attachment: signature.asc
Description: PGP signature

Reply via email to