Skip to content

Commit e2ab82f

Browse files
committed
Fix longopts_to_optstring(): use proper length of opts array
1 parent edfad8c commit e2ab82f

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ PG_CPPFLAGS = -I$(libpq_srcdir) ${PTHREAD_CFLAGS} -Isrc
3939
override CPPFLAGS := -DFRONTEND $(CPPFLAGS) $(PG_CPPFLAGS)
4040
PG_LIBS = $(libpq_pgport) ${PTHREAD_CFLAGS}
4141

42-
all: checksrcdir $(INCLUDES) $(PROGRAM);
42+
all: checksrcdir $(INCLUDES);
4343

4444
$(PROGRAM): $(OBJS)
45-
$(CC) $(CFLAGS) $(OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
4645

4746
src/xlogreader.c: $(top_srcdir)/src/backend/access/transam/xlogreader.c
4847
rm -f $@ && $(LN_S) $(srchome)/src/backend/access/transam/xlogreader.c $@

src/utils/pgut.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -669,22 +669,21 @@ parse_int(const char *value, int *result, int flags, const char **hintmsg)
669669
}
670670

671671
static char *
672-
longopts_to_optstring(const struct option opts[])
672+
longopts_to_optstring(const struct option opts[], const size_t len)
673673
{
674-
size_t len;
675-
char *result;
676-
char *s;
674+
size_t i;
675+
char *result;
676+
char *s;
677677

678-
for (len = 0; opts[len].name; len++) { }
679678
result = pgut_malloc(len * 2 + 1);
680679

681680
s = result;
682-
for (len = 0; opts[len].name; len++)
681+
for (i = 0; i < len; i++)
683682
{
684-
if (!isprint(opts[len].val))
683+
if (!isprint(opts[i].val))
685684
continue;
686-
*s++ = opts[len].val;
687-
if (opts[len].has_arg != no_argument)
685+
*s++ = opts[i].val;
686+
if (opts[i].has_arg != no_argument)
688687
*s++ = ':';
689688
}
690689
*s = '\0';
@@ -731,18 +730,18 @@ pgut_getopt_env(pgut_option options[])
731730
int
732731
pgut_getopt(int argc, char **argv, pgut_option options[])
733732
{
734-
int c;
735-
int optindex = 0;
736-
char *optstring;
737-
pgut_option *opt;
733+
int c;
734+
int optindex = 0;
735+
char *optstring;
736+
pgut_option *opt;
738737
struct option *longopts;
739-
size_t len1;
738+
size_t len;
740739

741-
len1 = option_length(options);
742-
longopts = pgut_newarray(struct option, len1 + 1);
743-
option_copy(longopts, options, len1);
740+
len = option_length(options);
741+
longopts = pgut_newarray(struct option, len + 1);
742+
option_copy(longopts, options, len);
744743

745-
optstring = longopts_to_optstring(longopts);
744+
optstring = longopts_to_optstring(longopts, len);
746745

747746
/* Assign named options */
748747
while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1)

0 commit comments

Comments
 (0)