Skip to content

Commit 67d9b56

Browse files
committed
Resolve int overflow cases
1 parent 9d4b8de commit 67d9b56

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

main.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define MAIN_H
33
#include <stdarg.h>
44
#include <string.h>
5-
#define ABS(x) ((x) < 0 ? -(x) : (x))
65
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
76
#define MAX_BYTE_SIZE 30
87

@@ -14,7 +13,7 @@ void print_error(char *);
1413
int parse_format(const char *);
1514

1615
void *alloc(size_t);
17-
char *int_to_string(int);
16+
char *int_to_string(long);
1817
int str_write(char *);
1918
int print_decimal(va_list);
2019

print_digits.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ int print_decimal(va_list ap)
1818
{
1919
int count = 0;
2020
int num = va_arg(ap, int);
21+
long big_num = (long) num;
2122
char *number_string;
2223

23-
if (num < 0)
24+
if (big_num < 0)
2425
{
2526
write(1, "-", 1);
2627
count++;
27-
num = ABS(num);
28+
big_num = labs(big_num);
2829
}
29-
if (num == 0)
30+
if (big_num == 0)
3031
return (write(1, "0", 1));
31-
number_string = int_to_string(num);
32+
number_string = int_to_string(big_num);
3233
count += str_write(number_string);
3334
free(number_string);
3435
va_end(ap);

print_helpers.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ void *alloc(size_t size)
3535
* Return: (num_str) char *
3636
*/
3737

38-
char *int_to_string(int num)
38+
char *int_to_string(long num)
3939
{
40-
int length = 1, tmp;
40+
long length = 1, tmp;
4141
char *num_str;
42-
int i;
42+
long i;
4343

4444
tmp = num;
4545

0 commit comments

Comments
 (0)