2
0
Эх сурвалжийг харах

Clean up a bunch of valgrind errors, nix a potentially unsafe op in Buffer assignment operator.

Adam Ierymenko 12 жил өмнө
parent
commit
f5d77a1bc2
2 өөрчлөгдсөн 18 нэмэгдсэн , 3 устгасан
  1. 10 1
      node/Buffer.hpp
  2. 8 2
      selftest.cpp

+ 10 - 1
node/Buffer.hpp

@@ -124,7 +124,7 @@ public:
 	{
 	{
 		if (b._l > C)
 		if (b._l > C)
 			throw std::out_of_range("Buffer: assignment from buffer larger than capacity");
 			throw std::out_of_range("Buffer: assignment from buffer larger than capacity");
-		memcpy(this,&b,sizeof(_l) + b._l); // one memcpy for all fields
+		memcpy(_b,b._b,_l = b._l);
 		return *this;
 		return *this;
 	}
 	}
 
 
@@ -357,6 +357,15 @@ public:
 		memset(_b + _l,0,C - _l);
 		memset(_b + _l,0,C - _l);
 	}
 	}
 
 
+	/**
+	 * Unconditionally zero buffer's underlying memory
+	 */
+	inline void zeroAll()
+		throw()
+	{
+		memset(_b,0,sizeof(_b));
+	}
+
 	/**
 	/**
 	 * @return Size of data in buffer
 	 * @return Size of data in buffer
 	 */
 	 */

+ 8 - 2
selftest.cpp

@@ -229,6 +229,9 @@ static int testPacket()
 	unsigned char salsaKey[32],hmacKey[32];
 	unsigned char salsaKey[32],hmacKey[32];
 	Packet a,b;
 	Packet a,b;
 
 
+	a.zeroAll();
+	b.zeroAll();
+
 	for(unsigned int i=0;i<32;++i) {
 	for(unsigned int i=0;i<32;++i) {
 		salsaKey[i] = (unsigned char)rand();
 		salsaKey[i] = (unsigned char)rand();
 		hmacKey[i] = (unsigned char)rand();
 		hmacKey[i] = (unsigned char)rand();
@@ -236,12 +239,15 @@ static int testPacket()
 
 
 	std::cout << "[packet] Testing Packet encoder/decoder... ";
 	std::cout << "[packet] Testing Packet encoder/decoder... ";
 
 
-	a = Packet();
-	a.setVerb(Packet::VERB_HELLO);
+	a.reset(Address(),Address(),Packet::VERB_HELLO);
 	for(int i=0;i<32;++i)
 	for(int i=0;i<32;++i)
 		a.append("supercalifragilisticexpealidocious",strlen("supercalifragilisticexpealidocious"));
 		a.append("supercalifragilisticexpealidocious",strlen("supercalifragilisticexpealidocious"));
 
 
 	b = a;
 	b = a;
+	if (a != b) {
+		std::cout << "FAIL (assign)" << std::endl;
+		return -1;
+	}
 
 
 	a.compress();
 	a.compress();
 	unsigned int complen = a.size();
 	unsigned int complen = a.size();