git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC Outreachy] Leftoverbits, which involve replacing atoi with strtol or strtol_i or equivalent.

From
Usman Akinyemi <usmanakinyemi202@gmail.com>
Date
Oct 12, 2024, 11:21 UTC
Message-ID
<CAPSxiM9FoGBhsnBE4E0Fvq-x_HvyFGGkkhaLNoRdVkvE6CpaZw@mail.gmail.com>
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.

Next: Taylor Blau
Message 1 of 8 in “[RFC Outreachy] Leftoverbits, which involve replacing atoi with strtol or strtol_i or equivalent.”
  1. Usman AkinyemiOct 12, 2024
  2. Taylor BlauOct 12, 2024
  3. Usman AkinyemiOct 13, 2024
  4. Taylor BlauOct 15, 2024
  5. Usman AkinyemiOct 15, 2024
  6. Usman AkinyemiOct 15, 2024
  7. Usman AkinyemiOct 15, 2024
  8. Usman AkinyemiOct 16, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.