[PATCH 1/1] cgitrc: handle value "0" for max-repo-count

Anthony LaTorre tlatorre at uchicago.edu
Thu Jan 10 20:23:26 CET 2019


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


More information about the CGit mailing list