git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Usman Akinyemi <usmanakinyemi202@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, Patrick Steinhardt <ps@pks.im>,
	phillip.wood123@gmail.com,
	 Christian Couder <christian.couder@gmail.com>,
	Eric Sunshine <sunshine@sunshineco.com>
Subject: [RFC Outreachy] Leftoverbits, which involve replacing atoi with strtol or strtol_i or equivalent.
Date: Sat, 12 Oct 2024 11:21:13 +0000	[thread overview]
Message-ID: <CAPSxiM9FoGBhsnBE4E0Fvq-x_HvyFGGkkhaLNoRdVkvE6CpaZw@mail.gmail.com> (raw)

Hello,

I was looking at some #leftoverbits which I can work on and I came
across this conversation.
https://public-inbox.org/git/CAC4O8c-nuOTS=a0sVp1603KaM2bZjs+yNZzdAaa5CGTNGFE7hQ@mail.gmail.com/

I followed the conversation and came accross
three instances where I think atoi can be
converted to strtol or strtol_i or parse_timestamp().
These are the three files which I think the atoi can be
replaced with.

merge-ll.c

something like this can be replace with
if (check->items[1].value) {
  marker_size = atoi(check->items[1].value);
  if (marker_size <= 0)
    marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
}

if (check->items[1].value) {
    char *endptr;
    long temp_marker_size = strtol(check->items[1].value, &endptr, 10);

    // Check for conversion errors
    if (endptr == check->items[1].value || *endptr != '\0' ||
temp_marker_size <= 0) {
        marker_size = DEFAULT_CONFLICT_MARKER_SIZE; // Set to default on error
    } else {
        marker_size = (int)temp_marker_size;
    }
}

alternative is to use strtol_i here which I think is more neater.
if (check->items[1].value) {
    if (strtol_i(check->items[1].value, 10, &marker_size) != 0 ||
marker_size <= 0) {
        marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
    }
}


daemon.c
if (skip_prefix(arg, "--timeout=", &v)) {
  timeout = atoi(v);
  continue;
}
if (skip_prefix(arg, "--init-timeout=", &v)) {
  init_timeout = atoi(v);
  continue;
}
if (skip_prefix(arg, "--max-connections=", &v)) {
  max_connections = atoi(v);
  if (max_connections < 0)
    max_connections = 0;         /* unlimited */
  continue;
}


if (skip_prefix(arg, "--timeout=", &v)) {
    timeout = parse_age(v);
    continue;
}

if (skip_prefix(arg, "--init-timeout=", &v)) {
    init_timeout = parse_age(v);
    continue;
}

if (skip_prefix(arg, "--max-connections=", &v)) {
    // Use strtol_i to convert the string to an integer
    if (strtol_i(v, 10, &max_connections) != 0 || max_connections < 0) {
        max_connections = 0; // Set to default on error
    }
    continue;
}

imap-send.c

if (!strcmp("UIDVALIDITY", arg)) {
  if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
    fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
    return RESP_BAD;
  }
} else if (!strcmp("UIDNEXT", arg)) {
  if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
    fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
    return RESP_BAD;
  }
} else if (!strcmp("CAPABILITY", arg)) {
  parse_capability(imap, s);
} else if (!strcmp("ALERT", arg)) {
  /* RFC2060 says that these messages MUST be displayed
   * to the user
   */
  for (; isspace((unsigned char)*p); p++);
  fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
} else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
  if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg)) ||
      !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
    fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
    return RESP_BAD;
  }
}

proposed one using strtol_i and I think instead of using strtol_i here, we can
have another custom function that indicate what cause the UIDVALIDITY
to be malformed
either overflow, letter etc

if (!strcmp("UIDVALIDITY", arg)) {
    if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &ctx->uidvalidity) != 0) {
        fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
        return RESP_BAD;
    }


} else if (!strcmp("UIDNEXT", arg)) {
    if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &imap->uidnext) != 0) {
        fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
        return RESP_BAD;
    }

} else if (!strcmp("CAPABILITY", arg)) {
    parse_capability(imap, s);
} else if (!strcmp("ALERT", arg)) {
    // RFC2060 says that these messages MUST be displayed to the user
    for (; isspace((unsigned char)*p); p++);
    fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
} else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
    if (!(arg = next_arg(&s)) ||
        (strtol_i(arg, 10, &ctx->uidvalidity) != 0) ||
        !(arg = next_arg(&s)) ||
        (strtol_i(arg, 10, (int *)cb->ctx) != 0)) {

        fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
        return RESP_BAD;
    }
}

- Another instance inside imap-send.c
} else {
    tag = atoi(arg);
    for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
        if (cmdp->tag == tag)
            goto gottag;
    fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
    return RESP_BAD;
}

- Possible solution.

} else {
    int tag_result; // Variable to hold the result of strtol_i
    if (strtol_i(arg, 10, &tag_result) != 0) {
        fprintf(stderr, "IMAP error: malformed tag %s\n", arg);
        return RESP_BAD;
    }

    // Now use the valid tag_result
    for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
        if (cmdp->tag == tag_result)
            goto gottag;

    fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
    return RESP_BAD;
}

Thank you.
Usman Akinyemi.

             reply	other threads:[~2024-10-12 11:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-12 11:21 Usman Akinyemi [this message]
2024-10-12 15:56 ` [RFC Outreachy] Leftoverbits, which involve replacing atoi with strtol or strtol_i or equivalent Taylor Blau
2024-10-13  3:46   ` Usman Akinyemi
2024-10-15  0:42     ` Taylor Blau
2024-10-15  3:49       ` Usman Akinyemi
2024-10-15  3:54         ` Usman Akinyemi
2024-10-15  4:04           ` Usman Akinyemi
2024-10-16  1:25             ` Usman Akinyemi

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=CAPSxiM9FoGBhsnBE4E0Fvq-x_HvyFGGkkhaLNoRdVkvE6CpaZw@mail.gmail.com \
    --to=usmanakinyemi202@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=phillip.wood123@gmail.com \
    --cc=ps@pks.im \
    --cc=sunshine@sunshineco.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).