Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
chore: use type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed May 28, 2024
1 parent e0f57e6 commit 902da4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/bencode_c/decode.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdint.h>

#include "common.h"
#include "overflow.h"
#include "str.h"
Expand Down Expand Up @@ -86,7 +88,7 @@ static PyObject *decodeInt(const char *buf, Py_ssize_t *index, Py_ssize_t size)
}

if (sign > 0) {
unsigned long long val = 0;
uint64_t val = 0;
for (Py_ssize_t i = *index; i < index_e; i++) {
char c = buf[i] - '0';
if (c < 0 || c > 9) {
Expand All @@ -97,8 +99,8 @@ static PyObject *decodeInt(const char *buf, Py_ssize_t *index, Py_ssize_t size)
// val = val * 10 + (buf[i] - '0')
// but with overflow check

int of = _u128_mul_overflow(val, 10, &val);
of = of || _u128_add_overflow(val, c, &val);
int of = _u64_mul_overflow(val, 10, &val);
of = of || _u64_add_overflow(val, c, &val);

if (of) {
goto __OverFlow;
Expand All @@ -109,7 +111,7 @@ static PyObject *decodeInt(const char *buf, Py_ssize_t *index, Py_ssize_t size)

return PyLong_FromUnsignedLongLong(val);
} else {
long long val = 0;
int64_t val = 0;
int of;
for (Py_ssize_t i = *index + 1; i < index_e; i++) {
char c = buf[i] - '0';
Expand All @@ -118,15 +120,15 @@ static PyObject *decodeInt(const char *buf, Py_ssize_t *index, Py_ssize_t size)
return NULL;
}

of = _i128_mul_overflow(val, 10, &val);
of = of || _i128_add_overflow(val, c, &val);
of = _i64_mul_overflow(val, 10, &val);
of = of || _i64_add_overflow(val, c, &val);

if (of) {
goto __OverFlow;
}
}

if (_i128_mul_overflow(val, sign, &val)) {
if (_i64_mul_overflow(val, sign, &val)) {
goto __OverFlow;
}

Expand Down
11 changes: 5 additions & 6 deletions src/bencode_c/overflow.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once
#include <limits.h>
#include <stdint.h>

// some helper to check int operator overflow

static int inline _u128_add_overflow(unsigned long long a, unsigned long long b,
unsigned long long *res) {
static int inline _u64_add_overflow(uint64_t a, uint64_t b, uint64_t *res) {
if (a > ULLONG_MAX - b) {
return -1;
}
Expand All @@ -13,8 +13,7 @@ static int inline _u128_add_overflow(unsigned long long a, unsigned long long b,
return 0;
}

static int inline _u128_mul_overflow(unsigned long long a, unsigned long long b,
unsigned long long *res) {
static int inline _u64_mul_overflow(uint64_t a, uint64_t b, uint64_t *res) {
if (a == 0 || b == 0) {
*res = 0;
return 0;
Expand All @@ -24,7 +23,7 @@ static int inline _u128_mul_overflow(unsigned long long a, unsigned long long b,
return !(a / b != *res);
}

static int inline _i128_add_overflow(long long a, long long b, long long *res) {
static int inline _i64_add_overflow(long long a, long long b, long long *res) {
if (a > 0 && b > LLONG_MAX - a) {
return -1;
} else if (a < 0 && b < LLONG_MAX - a) {
Expand All @@ -35,7 +34,7 @@ static int inline _i128_add_overflow(long long a, long long b, long long *res) {
return 0;
}

static int inline _i128_mul_overflow(long long a, long long b, long long *res) {
static int inline _i64_mul_overflow(long long a, long long b, long long *res) {
if (a == 0 || b == 0) {
*res = 0;
return 0;
Expand Down

0 comments on commit 902da4f

Please sign in to comment.