Skip to content

Commit 9a0efdf

Browse files
committed
On Win32, experiment with limiting overflow multiplication to 32-bit operands because the compiler runtime may not have an implementation.
1 parent acbe9c6 commit 9a0efdf

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

CoreFoundation/Base.subproj/CFOverflow.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,18 @@ CF_INLINE _CFOverflowResult _CFPointerSumWouldOverflow(void const *p, size_t n,
7575
return result;
7676
}
7777

78+
#if TARGET_OS_WIN32
79+
CF_INLINE bool _CFMultiplyBufferSizeWithoutOverflow(size_t a, size_t b, size_t *res) {
80+
int32_t res32 = 0;
81+
if (!os_mul_overflow((int32_t)a, (int32_t)b, &res32)) {
82+
*res = res32;
83+
return true;
84+
} else {
85+
return false;
86+
}
87+
}
88+
#else
89+
#define _CFMultiplyBufferSizeWithoutOverflow(a, b, res) (os_mul_overflow((a), (b), (res)) == 0)
90+
#endif
91+
7892
#endif /* CFOverflow_h */

CoreFoundation/URL.subproj/CFURLComponents_URIParser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ CF_EXPORT CFStringRef _CFStringCreateByAddingPercentEncodingWithAllowedCharacter
281281
else {
282282
// not big enough? malloc it.
283283
size_t mallocSize;
284-
if ( !os_mul_overflow(maxBufferSize, 4, &mallocSize) ) {
284+
if ( !_CFMultiplyBufferSizeWithoutOverflow(maxBufferSize, 4, &mallocSize) ) {
285285
inBuf = (UInt8 *)malloc(mallocSize);
286286
}
287287
}
@@ -456,7 +456,7 @@ CF_EXPORT CFStringRef _CFStringCreateByRemovingPercentEncoding(CFAllocatorRef al
456456
else {
457457
// not big enough? malloc it.
458458
size_t mallocSize;
459-
if ( !os_mul_overflow(maxBufferSize, 2, &mallocSize) ) {
459+
if ( !_CFMultiplyBufferSizeWithoutOverflow(maxBufferSize, 2, &mallocSize) ) {
460460
encodedBuf = (UInt8 *)malloc(mallocSize);
461461
}
462462
}

0 commit comments

Comments
 (0)