List for cgit developers and users
 help / color / mirror / Atom feed
From: tlatorre at uchicago.edu (Anthony LaTorre)
Subject: [PATCH 1/1] cgitrc: handle value "0" for max-repo-count
Date: Thu, 10 Jan 2019 13:23:26 -0600	[thread overview]
Message-ID: <CAO49af+VyhNz-a+4qG6NV37Vsd8RGjdXxXFxqdmAMd_g+jD8qA@mail.gmail.com> (raw)
In-Reply-To: <20190107163449.51f869a5@leda>

Here's a function I've been using to safely convert strings to ints
with error codes:

int safe_strtoi(char *s, int *si)
{
    /* Convert string s -> integer. */
    char *end;
    long value;

    errno = 0;
    value = strtol(s, &end, 0);

    if (end == s) {
        return -1;
    } else if ('\0' != *end) {
        return -1;
    } else if (errno == ERANGE) {
        return -1;
    } else if (value > INT_MAX) {
        return -1;
    } else if (value < INT_MIN) {
        return -1;
    }

    *si = value;

    return 0;
}

On success it returns 0 and si is set to the integer value. On error,
it returns -1.

Tony

On Mon, Jan 7, 2019 at 9:35 AM Christian Hesse <list at eworm.de> wrote:
>
> Christian Hesse <list at eworm.de> on Fri, 2018/11/23 17:08:
> > "Jason A. Donenfeld" <Jason at zx2c4.com> on Fri, 2018/10/19 00:52:
> > > Hey Christian,
> > >
> > > We should indeed introduce some sanitation helpers to deal with these
> > > in the general case. API suggestion:
> > >
> > > type_t parse_int(const char *str, type_t min, type_t max, type_t
> > > fallback_if_invalid);
> > >
> > > What would you think of that?
> >
> > My intention was to add a special value 0. How about this?
> >
> > type_t parse_int(const char *str, type_t min, type_t max, type_t
> > default_if_zero, type_t fallback_if_invalid);
>
> As atoi() does not return error there is no "invalid"... Looks like we have
> to go with:
>
> type_t parse_int(const char *str, type_t min, type_t max, type_t
> default_if_zero);
> --
> main(a){char*c=/*    Schoene Gruesse                         */"B?IJj;MEH"
> "CX:;",b;for(a/*    Best regards             my address:    */=0;b=c[a++];)
> putchar(b-1/(/*    Chris            cc -ox -xc - && ./x    */b/42*2-3)*42);}
> _______________________________________________
> CGit mailing list
> CGit at lists.zx2c4.com
> https://lists.zx2c4.com/mailman/listinfo/cgit


      parent reply	other threads:[~2019-01-10 19:23 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-16 14:38 list
2018-07-16 14:41 ` list
2018-09-11  6:56   ` list
2018-10-18 22:52     ` Jason
2018-11-23 16:08       ` list
2018-12-07  4:10         ` Jason
2019-01-07 15:34         ` list
2019-01-07 15:35           ` [PATCH 1/2] cgit: introduce parse_{bool, int}() for for cgitrc parsing list
2019-01-07 15:35             ` [PATCH 2/2] cgit: sanitize max-repo-count list
2019-01-07 20:54             ` [PATCH 1/2] cgit: introduce parse_{bool, int}() for for cgitrc parsing list
2019-01-08 14:05             ` whydoubt
2019-01-10 19:23           ` tlatorre [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAO49af+VyhNz-a+4qG6NV37Vsd8RGjdXxXFxqdmAMd_g+jD8qA@mail.gmail.com \
    --to=cgit@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).