git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC Outreachy] Leftoverbits, which involve replacing atoi with strtol or strtol_i or equivalent.
@ 2024-10-12 11:21 Usman Akinyemi
  2024-10-12 15:56 ` Taylor Blau
  0 siblings, 1 reply; 8+ messages in thread
From: Usman Akinyemi @ 2024-10-12 11:21 UTC (permalink / raw)
  To: git
  Cc: gitster, Patrick Steinhardt, phillip.wood123, Christian Couder,
	Eric Sunshine

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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-10-16  1:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-12 11:21 [RFC Outreachy] Leftoverbits, which involve replacing atoi with strtol or strtol_i or equivalent Usman Akinyemi
2024-10-12 15:56 ` 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

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).