No-Typo-Check: From a third party header file Bug: 260918793 Test: unit tests Test: atp v2/widevine-eng/drm_compliance Change-Id: I36effd6a10a99bdb2399ab1f4a0fad026d607c70
49 lines
857 B
C
49 lines
857 B
C
// Copyright 2019 Google LLC. All rights reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine
|
|
// License Agreement.
|
|
|
|
#include "odk_overflow.h"
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
int odk_sub_overflow_u64(uint64_t a, uint64_t b, uint64_t* c) {
|
|
if (a >= b) {
|
|
if (c) {
|
|
*c = a - b;
|
|
}
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int odk_add_overflow_u64(uint64_t a, uint64_t b, uint64_t* c) {
|
|
if (UINT64_MAX - a >= b) {
|
|
if (c) {
|
|
*c = a + b;
|
|
}
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int odk_add_overflow_ux(size_t a, size_t b, size_t* c) {
|
|
if (SIZE_MAX - a >= b) {
|
|
if (c) {
|
|
*c = a + b;
|
|
}
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int odk_mul_overflow_ux(size_t a, size_t b, size_t* c) {
|
|
if (b > 0 && a > SIZE_MAX / b) {
|
|
return 1;
|
|
}
|
|
if (c) {
|
|
*c = a * b;
|
|
}
|
|
return 0;
|
|
}
|