Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answeranswer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

  13. Variables/functions like INT_TO_STR_DIGITS_L[] and int_to_str() that are only intended for local use should be static. Unclear why all uppercase. Avoid long line that exceed presentation width

    // int INT_TO_STR_DIGITS_L[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    
  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

  13. Variables/functions like INT_TO_STR_DIGITS_L[] and int_to_str() that are only intended for local use should be static. Unclear why all uppercase. Avoid long line that exceed presentation width

    // int INT_TO_STR_DIGITS_L[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    
  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

  13. Variables/functions like INT_TO_STR_DIGITS_L[] and int_to_str() that are only intended for local use should be static. Unclear why all uppercase. Avoid long line that exceed presentation width

    // int INT_TO_STR_DIGITS_L[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    
added 456 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

  13. Variables/functions like INT_TO_STR_DIGITS_L[] and int_to_str() that are only intended for local use should be static. Unclear why all uppercase. Avoid long line that exceed presentation width

    // int INT_TO_STR_DIGITS_L[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    

static int int_to_str_digits_l[16] = "0123456789abcdef";

  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

  13. Variables/functions like INT_TO_STR_DIGITS_L[] and int_to_str() that are only intended for local use should be static. Unclear why all uppercase. Avoid long line that exceed presentation width

    // int INT_TO_STR_DIGITS_L[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    

static int int_to_str_digits_l[16] = "0123456789abcdef";

added 456 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answeranswer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed in another answer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  1. Bug: int_to_str() fails for negative int.

  2. Undefined specifiers: %h and %H are not part of the standard library. So without a specification, hard to know if they are performing correctly. Did you mean %x and %X?

  3. Mixing int and size_t math. This is pedantic point. As the max of those 2 types are not specified to which is larger, there is a worst case chance max_size - chars_printed <= 0 will never be true should max_size > INT_MAX. Suggest adding the ** line below and avoid math that relies on signed math as it is likely unsigned math. or use size_t chars_printed and cope with returning int at the end. (chars_printed should be of the type with the greater positive range.)

     int my_snprintf(char *str, size_t max_size, const char *fmt, ...) {
       if (max_size > INT_MAX) Handle_PathologicalCase_TBD(); // **
       int chars_printed = 0;
         ...
         // if (max_size - chars_printed <= 0) {
         if (max_size <= chars_printed) {
    
  4. Bug int length = (int)ceil(log((double)x)/log((double)base)); is not as reliable as hoped for. Detailed well in another answer. The alternative is to convert to a string with an internal max-sized buffer like char buf[34] for int32_t in base 2. Then copy the buffer result.

  5. Style: fall through. Cases that drop though look like an error without a break. Add comment to show intent

         case 'H':
             uppercase = 1;
             // fall though
         case 'h':
             base = 16;
             // fall though
         case 'd':
    
  6. printf("Invalid format.\n"); and such are better printed to stderr.

     fprintf(stderr, "Invalid format.\n");
    
  7. Wrong type for len

     // int len;
     size_t len;
    
  8. Style: No need to declare d so soon. Same for c. Suggest type change unsigned char c = va_arg(arg_list, int);. Same for str_arg.

     // int num;
     //  ... ~30 lines
     //    case 'd':
     //      num = va_arg(arg_list, int);
    
         case 'd':
             int num = va_arg(arg_list, int);
    
  9. Minor: Code simplification

     // if (uppercase) {
     //     c = INT_TO_STR_DIGITS_U[r];
     // ...
    
     if (uppercase) {
         c = "0123456789ABCDEF"[r];
     } else {
         c = "0123456789abcdef"[r];
     }
    
  10. Corner bug: Below code fails (UB). Watch for size == 0

     my_snprintf(str, 0, fmt, ...)
    
  11. [Edit] Bug: int uppercase = 0, base = 10; is initialized outside the for() loop. So a "%d" after a "%x" will be treated as hex. A "%h" after a "%H" will be treated as print with uppercase letters. Simple fix, move int uppercase = 0, base = 10; to after else if (fmt[i] == '%'). Better fix: pass base and upper/lower as parameters to new int_to_str().

  12. char *start_str unused. Recommend deletion.

added 27 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
Loading
deleted 1 character in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
Loading
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
Loading