Re: getopt() and strdup()

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: getopt() and strdup()
Date: 2012-10-10 23:54:15
Message-ID: 20121010235415.GE11890@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Oct 8, 2012 at 09:03:37PM -0400, Bruce Momjian wrote:
> On Mon, Oct 8, 2012 at 04:33:29PM -0400, Tom Lane wrote:
> > Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > > A while ago I noticed that in some places we strdup/pg_strdup() optarg
> > > strings from getopt(), and in some places we don't.
> >
> > > If we needed the strdup(), the missing cases should generate errors. If
> > > we don't need them, the strdup() is unnecessary, and research confirms
> > > they are unnecessary. Should we remove the extra strdup/pg_strdup()
> > > calls, for consistency.
> >
> > What research? Given the number of different ways argv[] is handled
> > on different platforms (cf ps_status.c), I am very unwilling to trust
> > that it's safe to hang onto an argv string for long without strdup'ing
> > it.
> >
> > > I think we might have had old platforms that required it, but none are
> > > still supported today.
> >
> > And what's your grounds for stating that? All the alternatives in
> > ps_status.c are still live code AFAICS.
> >
> > My feeling is it's more likely to be a good idea to be adding strdup's
> > than removing them.
>
> Well, what we have now is either wrong or over-kill --- I don't know for
> sure which.

OK, I have developed the attached patch to add strdup/pg_strdup() calls
to all saving of getopt optarg arguments.

Also, do we want to centralize the definition of pg_strdup() in /port,
or leave each module to define it on its own? I see pg_strdup() defined
in these modules:

/pgtop/contrib/oid2name
/pgtop/contrib/pgbench
/pgtop/contrib/pg_upgrade
/pgtop/src/bin/initdb
/pgtop/src/bin/pg_basebackup
/pgtop/src/bin/pg_ctl
/pgtop/src/bin/pg_dump
/pgtop/src/bin/psql
/pgtop/src/bin/scripts

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

Attachment Content-Type Size
getopt.diff text/x-diff 27.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2012-10-11 00:02:46 Re: [PATCH] Make pg_basebackup configure and start standby [Review]
Previous Message Bruce Momjian 2012-10-10 23:48:13 Re: [PATCH 8/8] Introduce wal decoding via catalog timetravel