[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gnulib-bug
Subject:    [PATCH] lib/argp-help.c: Corrected the default value and usage for inp_text_limit in argp_doc()
From:       Girish Joshi <girish946 () gmail ! com>
Date:       2021-02-07 19:29:15
Message-ID: CALkY8p9=dtvp7LcCsd9Zvn+psM8fuYXrg5jGzwjeR9v8XmanYQ () mail ! gmail ! com
[Download RAW message or body]

Hello,
This is in the reference to bugzilla entry #19038[1] for glibc.
It was suggested to submit this patch on this mailing list as the argp
module is shared in gnulib.

The details for the bug and the patch are as follows.

Overview:
argp.doc prints incorrectly when it starts with '\v'.
In argp-help.c in the function argp_doc() variable inp_text_limit is reset to 0
if the doc string starts with '\v'. Which causes the whole doc string to be
printed in the case of pre documentation, because of initialization of inp_text
and inp_text_limit

    inp_text = post ? (vt ? vt + 1 : 0) : doc;
    inp_text_limit = (!post && vt) ? (vt - doc) : 0;

and the condition where the doc string is printed.

    if (text == inp_text && inp_text_limit)
      __argp_fmtstream_write (stream, inp_text, inp_text_limit);

So for the following code

    #include<argp.h>

    static char doc[] = "\vthis is post_doc";
    static struct argp argp = {NULL, NULL, NULL, doc};

    int main(int argc, char *args[]){
         argp_parse(&argp, argc, args, 0, 0, NULL);
    }

the output is

    $ argp-help --help
    Usage: argp-help [OPTION...]

    this is post_doc

      -?, --help                 Give this help list
          --usage                Give a short usage message

    this is post_doc

As mentioned in the bugzilla entry the first occurrence of
"this is post_doc" is erroneous as it is the pre doc and there is nothing
in the doc string in the predoc section.

Implementation:
Reset the value of inp_text_limit to -1 if the doc string starts with '\v'.
Modify the condition for printing the complete doc string with validation for
inp_text_limit variable which looks like.

    if (text == inp_text && inp_text_limit != -1)
      __argp_fmtstream_write (stream, inp_text, inp_text_limit);

after applying this patch we get the output as following

    $ argp-help --help
    Usage: argp-help [OPTION...]

      -?, --help                 Give this help list
          --usage                Give a short usage message

    this is post_doc

Could someone please review this patch?

Thanks.
Girish Joshi

[1]: https://sourceware.org/bugzilla/show_bug.cgi?id=19038

["0001-lib-argp-help.c-Corrected-the-default-value-and-usag.patch" (text/x-patch)]

From 6eae4da9747559c1bfd7fedebe8c0dda9bf5bce6 Mon Sep 17 00:00:00 2001
From: Girish Joshi <girish946@gmail.com>
Date: Mon, 8 Feb 2021 00:39:26 +0530
Subject: [PATCH] lib/argp-help.c: Corrected the default value and usage for
 inp_text_limit in argp_doc()

This fixes an old glibc bug
<https://sourceware.org/bugzilla/show_bug.cgi?id=19038>
---
 lib/argp-help.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/argp-help.c b/lib/argp-help.c
index ac1804d89..e9ac4a19d 100644
--- a/lib/argp-help.c
+++ b/lib/argp-help.c
@@ -1590,7 +1590,7 @@ argp_doc (const struct argp *argp, const struct argp_state *state,
   const char *inp_text;
   void *input = 0;
   int anything = 0;
-  size_t inp_text_limit = 0;
+  size_t inp_text_limit = -1;
   const char *doc = argp->doc ? dgettext (argp->argp_domain, argp->doc) : NULL;
   const struct argp_child *child = argp->children;
 
@@ -1598,7 +1598,7 @@ argp_doc (const struct argp *argp, const struct argp_state *state,
     {
       char *vt = strchr (doc, '\v');
       inp_text = post ? (vt ? vt + 1 : 0) : doc;
-      inp_text_limit = (!post && vt) ? (vt - doc) : 0;
+      inp_text_limit = (!post && vt) ? (vt - doc) : -1;
     }
   else
     inp_text = 0;
@@ -1624,7 +1624,7 @@ argp_doc (const struct argp *argp, const struct argp_state *state,
       if (pre_blank)
         __argp_fmtstream_putc (stream, '\n');
 
-      if (text == inp_text && inp_text_limit)
+      if (text == inp_text && inp_text_limit != -1)
         __argp_fmtstream_write (stream, inp_text, inp_text_limit);
       else
         __argp_fmtstream_puts (stream, text);
-- 
2.26.2



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic