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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx/

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.




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux