Re: [PATCH v1] cyclictest: Generate optstring dynamically from long_options

Crystal Wood
2026-01-13T23:34:27Z

On Tue, 2026-01-13 at 09:13 +0100, Sebastian Andrzej Siewior wrote:
> On 2026-01-12 15:13:22 [-0600], Crystal Wood wrote:
> > > --- /dev/null
> > > +++ b/src/lib/opt.c
> > > @@ -0,0 +1,29 @@
> > > +// SPDX-License-Identifier: GPL-2.0-or-later
> > > +#include <stdlib.h>
> > > +
> > > +#include "opt.h"
> > > +
> > > +char *build_optstring(const struct option *long_opts)
> > > +{
> > > + char *opts = NULL;
> > > + int n = 0;
> > > +
> > > + for (int i = 0; long_opts[i].name; i++) {
> > > + opts = realloc(opts, n + 4);
> >
> > I realize this isn't going to be noticeable even if it ends up being a
> > copy each time, but linear realloc like this makes me twinge and think
> > of how just about every other language in common use would have a
> > readily available growable string implementation :-P
>
> and assume you never run out of memory…

Exceptions are a thing, though in practice a small allocation failing
means things are pretty screwed regardless. And it's not like each of
those error paths is getting test coverage... For most non-critical
userspace code, might as well keep things simple and crash out on OOM
(with top-level exception/atexit cleanup in one place, if needed).

-Crystal