Skip to content

Commit

Permalink
CORE-249: In Core Ethereum, address Xcode Static Analysis issues #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Gamble committed Feb 18, 2019
1 parent faca2d2 commit ffbb45b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
8 changes: 8 additions & 0 deletions ethereum/blockchain/BREthereumBlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,10 @@ blockTransactionsRlpEncode (BREthereumBlock block,
BREthereumRlpType type,
BRRlpCoder coder) {
size_t itemsCount = (NULL == block->transactions ? 0 : array_count(block->transactions));

// If there are no items, skip out immediately.
if (0 == itemsCount) return rlpEncodeList(coder, 0);

BRRlpItem items[itemsCount];

for (int i = 0; i < itemsCount; i++)
Expand Down Expand Up @@ -972,6 +976,10 @@ blockOmmersRlpEncode (BREthereumBlock block,
BREthereumRlpType type,
BRRlpCoder coder) {
size_t itemsCount = (NULL == block->ommers ? 0 : array_count(block->ommers));

// If there are no items, skip out immediately.
if (0 == itemsCount) return rlpEncodeList(coder, 0);

BRRlpItem items[itemsCount];

for (int i = 0; i < itemsCount; i++)
Expand Down
41 changes: 12 additions & 29 deletions ethereum/les/BREthereumLES.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,6 @@ lesThread (BREthereumLES les) {
fd_set readDescriptors, writeDesciptors;
int maximumDescriptor = -1;

// True if we need to update descriptors.
int updateDesciptors = 1;

pthread_mutex_lock (&les->lock);

BRArrayOf(BREthereumNode) nodesToRemove;
Expand Down Expand Up @@ -1204,28 +1201,21 @@ lesThread (BREthereumLES les) {
for (ssize_t index = requestsToFailCount - 1; index >= 0; index--)
array_rm (les->requests, requestsToFail[index]);

// Just do it, always.
updateDesciptors = 1;

//
// Update the read (and write) descriptors to include nodes that are 'active' on any route.
//
if (updateDesciptors) {
maximumDescriptor = -1;
FD_ZERO (&readDescriptors);
FD_ZERO (&writeDesciptors);

FOR_EACH_ROUTE(route) {
BRArrayOf(BREthereumNode) nodes = les->activeNodesByRoute[route];
for (size_t index = 0; index < array_count(nodes); index++)
maximumDescriptor = maximum (maximumDescriptor,
nodeUpdateDescriptors (nodes[index],
route,
&readDescriptors,
&writeDesciptors));
}
maximumDescriptor = -1;
FD_ZERO (&readDescriptors);
FD_ZERO (&writeDesciptors);

// updateDesciptors = 0;
FOR_EACH_ROUTE(route) {
BRArrayOf(BREthereumNode) nodes = les->activeNodesByRoute[route];
for (size_t index = 0; index < array_count(nodes); index++)
maximumDescriptor = maximum (maximumDescriptor,
nodeUpdateDescriptors (nodes[index],
route,
&readDescriptors,
&writeDesciptors));
}

pthread_mutex_unlock (&les->lock);
Expand Down Expand Up @@ -1376,19 +1366,12 @@ lesThread (BREthereumLES les) {
assert (0); // how?
}
}

// updateDesciptors = 1;

// pipe ()
}

//
// or we have an pselect() error.
//
else {
lesHandleSelectError (les, errno);
updateDesciptors = 1;
}
else lesHandleSelectError (les, errno);

// double check that everything has been handled.
assert (0 == array_count(nodesToRemove));
Expand Down
21 changes: 9 additions & 12 deletions ethereum/les/BREthereumNode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,6 @@ nodeSend (BREthereumNode node,
BREthereumMessage message) {

int error = 0;
size_t bytesCount = 0;

assert ((NODE_ROUTE_UDP == route && MESSAGE_DIS == message.identifier) ||
(NODE_ROUTE_UDP != route && MESSAGE_DIS != message.identifier));
Expand All @@ -2040,7 +2039,6 @@ nodeSend (BREthereumNode node,
pthread_mutex_lock (&node->lock);
error = nodeEndpointSendData (node->remote, route, data.bytes, data.bytesCount);
pthread_mutex_unlock (&node->lock);
bytesCount = data.bytesCount;
break;
}

Expand All @@ -2064,7 +2062,6 @@ nodeSend (BREthereumNode node,

error = nodeEndpointSendData (node->remote, route, encryptedData.bytes, encryptedData.bytesCount);
pthread_mutex_unlock (&node->lock);
bytesCount = encryptedData.bytesCount;
rlpDataRelease(encryptedData);
break;
}
Expand Down Expand Up @@ -2157,7 +2154,7 @@ nodeRecv (BREthereumNode node,
node->recvDataBuffer.bytesCount = bytesCount;
node->recvDataBuffer.bytes = realloc(node->recvDataBuffer.bytes, bytesCount);
bytes = node->recvDataBuffer.bytes;
bytesLimit = bytesCount;
// bytesLimit = bytesCount;
}
pthread_mutex_unlock (&node->lock);

Expand Down Expand Up @@ -2392,17 +2389,17 @@ _sendAuthInitiator(BREthereumNode node) {
memset(xorStaticNonce.u8, 0, 32);
bytesXOR(staticSharedSecret.u8, localNonce->u8, xorStaticNonce.u8, sizeof(localNonce->u8));


// S(ephemeral-privk, static-shared-secret ^ nonce)
// Determine the signature length
size_t signatureLen = 65; BRKeyCompactSignEthereum(localEphemeral,
NULL, 0,
xorStaticNonce);

size_t signatureLen = BRKeyCompactSignEthereum (localEphemeral,
NULL, 0,
xorStaticNonce);
assert (65 == signatureLen);

// Fill the signature
signatureLen = BRKeyCompactSignEthereum(localEphemeral,
signature, signatureLen,
xorStaticNonce);
BRKeyCompactSignEthereum (localEphemeral,
signature, signatureLen,
xorStaticNonce);

// || H(ephemeral-pubk)||
memset(&hPubKey[32], 0, 32);
Expand Down
12 changes: 9 additions & 3 deletions ethereum/util/BRUtilHex.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@
//
//

// Convert a char into uint8_t (decode)
#define decodeChar(c) ((uint8_t) _hexu(c))

// Convert a uint8_t into a char (encode)
#define encodeChar(u) ((char) _hexc(u))

extern void
decodeHex (uint8_t *target, size_t targetLen, const char *source, size_t sourceLen) {
//
assert (0 == sourceLen % 2);
assert (2 * targetLen == sourceLen);

for (int i = 0; i < targetLen; i++) {
target[i] = (uint8_t) ((_hexu(source[2*i]) << 4) | _hexu(source[(2*i)+1]));
target[i] = (uint8_t) ((decodeChar(source[2*i]) << 4) | decodeChar(source[(2*i)+1]));
}
}

Expand All @@ -65,8 +71,8 @@ encodeHex (char *target, size_t targetLen, const uint8_t *source, size_t sourceL
assert (targetLen == 2 * sourceLen + 1);

for (int i = 0; i < sourceLen; i++) {
target[2*i] = (uint8_t) _hexc (source[i] >> 4);
target[2*i + 1] = (uint8_t) _hexc (source[i]);
target[2*i + 0] = encodeChar (source[i] >> 4);
target[2*i + 1] = encodeChar (source[i]);
}
target[2*sourceLen] = '\0';
}
Expand Down

0 comments on commit ffbb45b

Please sign in to comment.