Fix overloaded operators in bigint.
Assignment and increment/decrement operators should return values.
PiperOrigin-RevId: 254821537
Change-Id: I85d2fe7669474cb27850b7e4196edb424dd87ad1
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/56891
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/third_party/bigint/BigInteger.cc b/third_party/bigint/BigInteger.cc
index 93b57e4..476f3e6 100644
--- a/third_party/bigint/BigInteger.cc
+++ b/third_party/bigint/BigInteger.cc
@@ -6,14 +6,15 @@
#include "BigInteger.hh"
-void BigInteger::operator =(const BigInteger &x) {
+BigInteger& BigInteger::operator =(const BigInteger &x) {
// Calls like a = a have no effect
if (this == &x)
- return;
+ return *this;
// Copy sign
sign = x.sign;
// Copy the rest
mag = x.mag;
+ return *this;
}
BigInteger::BigInteger(const Blk *b, Index blen, Sign s) : mag(b, blen) {
@@ -374,7 +375,7 @@
// INCREMENT/DECREMENT OPERATORS
// Prefix increment
-void BigInteger::operator ++() {
+BigInteger& BigInteger::operator ++() {
if (sign == negative) {
mag--;
if (mag == 0)
@@ -383,15 +384,18 @@
mag++;
sign = positive; // if not already
}
+ return *this;
}
-// Postfix increment: same as prefix
-void BigInteger::operator ++(int) {
+// Postfix increment
+BigInteger BigInteger::operator ++(int) {
+ BigInteger temp(*this);
operator ++();
+ return temp;
}
// Prefix decrement
-void BigInteger::operator --() {
+BigInteger& BigInteger::operator --() {
if (sign == positive) {
mag--;
if (mag == 0)
@@ -400,10 +404,13 @@
mag++;
sign = negative;
}
+ return *this;
}
-// Postfix decrement: same as prefix
-void BigInteger::operator --(int) {
+// Postfix decrement
+BigInteger BigInteger::operator --(int) {
+ BigInteger temp(*this);
operator --();
+ return temp;
}
diff --git a/third_party/bigint/BigInteger.hh b/third_party/bigint/BigInteger.hh
index 35a2b6b..b5d3518 100644
--- a/third_party/bigint/BigInteger.hh
+++ b/third_party/bigint/BigInteger.hh
@@ -41,7 +41,7 @@
BigInteger(const BigInteger &x) : sign(x.sign), mag(x.mag) {}
// Assignment operator
- void operator=(const BigInteger &x);
+ BigInteger& operator=(const BigInteger &x);
// Constructor that copies from a given array of blocks with a sign.
BigInteger(const Blk *b, Index blen, Sign s);
@@ -129,18 +129,18 @@
BigInteger operator %(const BigInteger &x) const;
BigInteger operator -() const;
- void operator +=(const BigInteger &x);
- void operator -=(const BigInteger &x);
- void operator *=(const BigInteger &x);
- void operator /=(const BigInteger &x);
- void operator %=(const BigInteger &x);
+ BigInteger& operator +=(const BigInteger &x);
+ BigInteger& operator -=(const BigInteger &x);
+ BigInteger& operator *=(const BigInteger &x);
+ BigInteger& operator /=(const BigInteger &x);
+ BigInteger& operator %=(const BigInteger &x);
void flipSign();
// INCREMENT/DECREMENT OPERATORS
- void operator ++( );
- void operator ++(int);
- void operator --( );
- void operator --(int);
+ BigInteger& operator ++( );
+ BigInteger operator ++(int);
+ BigInteger& operator --( );
+ BigInteger operator --(int);
};
// NORMAL OPERATORS
@@ -191,16 +191,19 @@
* belongs to the put-here operations. See Assignment Operators in
* BigUnsigned.hh.
*/
-inline void BigInteger::operator +=(const BigInteger &x) {
+inline BigInteger& BigInteger::operator +=(const BigInteger &x) {
add(*this, x);
+ return *this;
}
-inline void BigInteger::operator -=(const BigInteger &x) {
+inline BigInteger& BigInteger::operator -=(const BigInteger &x) {
subtract(*this, x);
+ return *this;
}
-inline void BigInteger::operator *=(const BigInteger &x) {
+inline BigInteger& BigInteger::operator *=(const BigInteger &x) {
multiply(*this, x);
+ return *this;
}
-inline void BigInteger::operator /=(const BigInteger &x) {
+inline BigInteger& BigInteger::operator /=(const BigInteger &x) {
if (x.isZero())
abort();
/* The following technique is slightly faster than copying *this first
@@ -209,13 +212,15 @@
divideWithRemainder(x, q);
// *this contains the remainder, but we overwrite it with the quotient.
*this = q;
+ return *this;
}
-inline void BigInteger::operator %=(const BigInteger &x) {
+inline BigInteger& BigInteger::operator %=(const BigInteger &x) {
if (x.isZero())
abort();
BigInteger q;
// Mods *this by x. Don't care about quotient left in q.
divideWithRemainder(x, q);
+ return *this;
}
// This one is trivial
inline void BigInteger::flipSign() {
diff --git a/third_party/bigint/BigUnsigned.cc b/third_party/bigint/BigUnsigned.cc
index e38e4aa..0713632 100644
--- a/third_party/bigint/BigUnsigned.cc
+++ b/third_party/bigint/BigUnsigned.cc
@@ -659,7 +659,7 @@
// INCREMENT/DECREMENT OPERATORS
// Prefix increment
-void BigUnsigned::operator ++() {
+BigUnsigned& BigUnsigned::operator ++() {
Index i;
bool carry = true;
for (i = 0; i < len && carry; i++) {
@@ -672,15 +672,18 @@
len++;
blk[i] = 1;
}
+ return *this;
}
-// Postfix increment: same as prefix
-void BigUnsigned::operator ++(int) {
+// Postfix increment
+BigUnsigned BigUnsigned::operator ++(int) {
+ BigUnsigned temp(*this);
operator ++();
+ return temp;
}
// Prefix decrement
-void BigUnsigned::operator --() {
+BigUnsigned& BigUnsigned::operator --() {
if (len == 0)
abort();
Index i;
@@ -692,9 +695,12 @@
// Zap possible leading zero (there can only be one)
if (blk[len - 1] == 0)
len--;
+ return *this;
}
-// Postfix decrement: same as prefix
-void BigUnsigned::operator --(int) {
+// Postfix decrement
+BigUnsigned BigUnsigned::operator --(int) {
+ BigUnsigned temp(*this);
operator --();
+ return temp;
}
diff --git a/third_party/bigint/BigUnsigned.hh b/third_party/bigint/BigUnsigned.hh
index de4c18e..56da651 100644
--- a/third_party/bigint/BigUnsigned.hh
+++ b/third_party/bigint/BigUnsigned.hh
@@ -46,8 +46,9 @@
BigUnsigned(const BigUnsigned &x) : NumberlikeArray<Blk>(x) {}
// Assignment operator
- void operator=(const BigUnsigned &x) {
+ BigUnsigned& operator=(const BigUnsigned &x) {
NumberlikeArray<Blk>::operator =(x);
+ return *this;
}
// Constructor that copies from a given array of blocks.
@@ -218,24 +219,24 @@
BigUnsigned operator >>(int b) const;
// OVERLOADED ASSIGNMENT OPERATORS
- void operator +=(const BigUnsigned &x);
- void operator -=(const BigUnsigned &x);
- void operator *=(const BigUnsigned &x);
- void operator /=(const BigUnsigned &x);
- void operator %=(const BigUnsigned &x);
- void operator &=(const BigUnsigned &x);
- void operator |=(const BigUnsigned &x);
- void operator ^=(const BigUnsigned &x);
- void operator <<=(int b);
- void operator >>=(int b);
+ BigUnsigned& operator +=(const BigUnsigned &x);
+ BigUnsigned& operator -=(const BigUnsigned &x);
+ BigUnsigned& operator *=(const BigUnsigned &x);
+ BigUnsigned& operator /=(const BigUnsigned &x);
+ BigUnsigned& operator %=(const BigUnsigned &x);
+ BigUnsigned& operator &=(const BigUnsigned &x);
+ BigUnsigned& operator |=(const BigUnsigned &x);
+ BigUnsigned& operator ^=(const BigUnsigned &x);
+ BigUnsigned& operator <<=(int b);
+ BigUnsigned& operator >>=(int b);
/* INCREMENT/DECREMENT OPERATORS
* To discourage messy coding, these do not return *this, so prefix
* and postfix behave the same. */
- void operator ++( );
- void operator ++(int);
- void operator --( );
- void operator --(int);
+ BigUnsigned& operator ++( );
+ BigUnsigned operator ++(int);
+ BigUnsigned& operator --( );
+ BigUnsigned operator --(int);
// Helper function that needs access to BigUnsigned internals
friend Blk getShiftedBlock(const BigUnsigned &num, Index x,
@@ -307,16 +308,19 @@
return ans;
}
-inline void BigUnsigned::operator +=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator +=(const BigUnsigned &x) {
add(*this, x);
+ return *this;
}
-inline void BigUnsigned::operator -=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator -=(const BigUnsigned &x) {
subtract(*this, x);
+ return *this;
}
-inline void BigUnsigned::operator *=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator *=(const BigUnsigned &x) {
multiply(*this, x);
+ return *this;
}
-inline void BigUnsigned::operator /=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator /=(const BigUnsigned &x) {
if (x.isZero())
abort();
/* The following technique is slightly faster than copying *this first
@@ -325,28 +329,35 @@
divideWithRemainder(x, q);
// *this contains the remainder, but we overwrite it with the quotient.
*this = q;
+ return *this;
}
-inline void BigUnsigned::operator %=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator %=(const BigUnsigned &x) {
if (x.isZero())
abort();
BigUnsigned q;
// Mods *this by x. Don't care about quotient left in q.
divideWithRemainder(x, q);
+ return *this;
}
-inline void BigUnsigned::operator &=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator &=(const BigUnsigned &x) {
bitAnd(*this, x);
+ return *this;
}
-inline void BigUnsigned::operator |=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator |=(const BigUnsigned &x) {
bitOr(*this, x);
+ return *this;
}
-inline void BigUnsigned::operator ^=(const BigUnsigned &x) {
+inline BigUnsigned& BigUnsigned::operator ^=(const BigUnsigned &x) {
bitXor(*this, x);
+ return *this;
}
-inline void BigUnsigned::operator <<=(int b) {
+inline BigUnsigned& BigUnsigned::operator <<=(int b) {
bitShiftLeft(*this, b);
+ return *this;
}
-inline void BigUnsigned::operator >>=(int b) {
+inline BigUnsigned& BigUnsigned::operator >>=(int b) {
bitShiftRight(*this, b);
+ return *this;
}
/* Templates for conversions of BigUnsigned to and from primitive integers.
diff --git a/third_party/bigint/NumberlikeArray.hh b/third_party/bigint/NumberlikeArray.hh
index f791780..df58dae 100644
--- a/third_party/bigint/NumberlikeArray.hh
+++ b/third_party/bigint/NumberlikeArray.hh
@@ -70,7 +70,7 @@
NumberlikeArray(const NumberlikeArray<Blk> &x);
// Assignment operator
- void operator=(const NumberlikeArray<Blk> &x);
+ NumberlikeArray<Blk>& operator=(const NumberlikeArray<Blk> &x);
// Constructor that copies from a given array of blocks
NumberlikeArray(const Blk *b, Index blen);
@@ -139,11 +139,11 @@
}
template <class Blk>
-void NumberlikeArray<Blk>::operator=(const NumberlikeArray<Blk> &x) {
+NumberlikeArray<Blk>& NumberlikeArray<Blk>::operator=(const NumberlikeArray<Blk> &x) {
/* Calls like a = a have no effect; catch them before the aliasing
* causes a problem */
if (this == &x)
- return;
+ return *this;
// Copy length
len = x.len;
// Expand array if necessary
@@ -152,6 +152,7 @@
Index i;
for (i = 0; i < len; i++)
blk[i] = x.blk[i];
+ return *this;
}
template <class Blk>