Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a few null checks for NewWithAvailableSize #3123

Merged
merged 3 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/chip-tool/commands/common/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ void ModelCommand::SendCommand(ChipDeviceController * dc)
// Make sure our buffer is big enough, but this will need a better setup!
static const size_t bufferSize = 1024;
auto * buffer = PacketBuffer::NewWithAvailableSize(bufferSize);
uint16_t dataLength = EncodeCommand(buffer, bufferSize, mEndPointId);

if (buffer == nullptr)
{
ChipLogError(chipTool, "Failed to allocate memory for packet data.");
return;
}

uint16_t dataLength = EncodeCommand(buffer, bufferSize, mEndPointId);
buffer->SetDataLength(dataLength);
ChipLogProgress(chipTool, "Encoded data of length %d", dataLength);

Expand Down
159 changes: 97 additions & 62 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,39 @@ static CHIP_ERROR N2J_Error(JNIEnv * env, CHIP_ERROR inErr, jthrowable & outEx);
static CHIP_ERROR N2J_NewStringUTF(JNIEnv * env, const char * inStr, jstring & outString);
static CHIP_ERROR N2J_NewStringUTF(JNIEnv * env, const char * inStr, size_t inStrLen, jstring & outString);

static JavaVM * sJVM;
static System::Layer sSystemLayer;
static Inet::InetLayer sInetLayer;
namespace {

JavaVM * sJVM;
System::Layer sSystemLayer;
Inet::InetLayer sInetLayer;

#if CONFIG_NETWORK_LAYER_BLE
static Ble::BleLayer sBleLayer;
static AndroidBleApplicationDelegate sBleApplicationDelegate;
static AndroidBlePlatformDelegate sBlePlatformDelegate;
static AndroidBleConnectionDelegate sBleConnectionDelegate;
Ble::BleLayer sBleLayer;
AndroidBleApplicationDelegate sBleApplicationDelegate;
AndroidBlePlatformDelegate sBlePlatformDelegate;
AndroidBleConnectionDelegate sBleConnectionDelegate;
#endif
static pthread_mutex_t sStackLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_t sIOThread = PTHREAD_NULL;
static bool sShutdown = false;

static jclass sAndroidChipStackCls = NULL;
static jclass sChipDeviceControllerCls = NULL;
static jclass sChipDeviceControllerExceptionCls = NULL;
pthread_mutex_t sStackLock = PTHREAD_MUTEX_INITIALIZER;
pthread_t sIOThread = PTHREAD_NULL;
bool sShutdown = false;

jclass sAndroidChipStackCls = NULL;
jclass sChipDeviceControllerCls = NULL;
jclass sChipDeviceControllerExceptionCls = NULL;

/** A scoped lock/unlock around a mutex. */
class ScopedPthreadLock
{
public:
ScopedPthreadLock(pthread_mutex_t * mutex) : mMutex(mutex) { pthread_mutex_lock(mMutex); }
~ScopedPthreadLock() { pthread_mutex_unlock(mMutex); }

private:
pthread_mutex_t * mMutex;
};

} // namespace

// NOTE: Remote device ID is in sync with the echo server device id
// At some point, we may want to add an option to connect to a device without
Expand Down Expand Up @@ -245,15 +262,16 @@ JNI_METHOD(void, beginConnectDevice)(JNIEnv * env, jobject self, jlong deviceCon

ChipLogProgress(Controller, "beginConnectDevice() called with connection object and pincode");

pthread_mutex_lock(&sStackLock);
sBleLayer.mAppState = appReqState;
RendezvousParameters params = RendezvousParameters()
.SetSetupPINCode(pinCode)
.SetConnectionObject(reinterpret_cast<BLE_CONNECTION_OBJECT>(connObj))
.SetBleLayer(&sBleLayer);
err = deviceController->ConnectDevice(kRemoteDeviceId, params, appReqState, HandleKeyExchange, HandleEchoResponse, HandleError);

pthread_mutex_unlock(&sStackLock);
{
ScopedPthreadLock lock(&sStackLock);
sBleLayer.mAppState = appReqState;
RendezvousParameters params = RendezvousParameters()
.SetSetupPINCode(pinCode)
.SetConnectionObject(reinterpret_cast<BLE_CONNECTION_OBJECT>(connObj))
.SetBleLayer(&sBleLayer);
err = deviceController->ConnectDevice(kRemoteDeviceId, params, appReqState, HandleKeyExchange, HandleEchoResponse,
HandleError);
}

if (err != CHIP_NO_ERROR)
{
Expand All @@ -274,10 +292,11 @@ JNI_METHOD(void, beginConnectDeviceIp)(JNIEnv * env, jobject self, jlong deviceC
deviceIPAddr.FromString(deviceAddrStr, deviceIPAddr);
env->ReleaseStringUTFChars(deviceAddr, deviceAddrStr);

pthread_mutex_lock(&sStackLock);
err = deviceController->ConnectDeviceWithoutSecurePairing(kRemoteDeviceId, deviceIPAddr, (void *) "ConnectDevice",
HandleKeyExchange, HandleEchoResponse, HandleError, CHIP_PORT);
pthread_mutex_unlock(&sStackLock);
{
ScopedPthreadLock lock(&sStackLock);
err = deviceController->ConnectDeviceWithoutSecurePairing(kRemoteDeviceId, deviceIPAddr, (void *) "ConnectDevice",
HandleKeyExchange, HandleEchoResponse, HandleError, CHIP_PORT);
}

if (err != CHIP_NO_ERROR)
{
Expand All @@ -296,14 +315,21 @@ JNI_METHOD(void, beginSendMessage)(JNIEnv * env, jobject self, jlong deviceContr
const char * messageStr = env->GetStringUTFChars(messageObj, 0);
size_t messageLen = strlen(messageStr);

pthread_mutex_lock(&sStackLock);

auto * buffer = System::PacketBuffer::NewWithAvailableSize(messageLen);
memcpy(buffer->Start(), messageStr, messageLen);
buffer->SetDataLength(messageLen);
err = deviceController->SendMessage((void *) "SendMessage", buffer);
{
ScopedPthreadLock lock(&sStackLock);

pthread_mutex_unlock(&sStackLock);
auto * buffer = System::PacketBuffer::NewWithAvailableSize(messageLen);
if (buffer == nullptr)
{
err = CHIP_ERROR_NO_MEMORY;
}
else
{
memcpy(buffer->Start(), messageStr, messageLen);
buffer->SetDataLength(messageLen);
err = deviceController->SendMessage((void *) "SendMessage", buffer);
}
}

env->ReleaseStringUTFChars(messageObj, messageStr);

Expand All @@ -325,38 +351,46 @@ JNI_METHOD(void, beginSendCommand)(JNIEnv * env, jobject self, jlong deviceContr
jmethodID commandMethodID = env->GetMethodID(commandCls, "getValue", "()I");
jint commandID = env->CallIntMethod(commandObj, commandMethodID);

pthread_mutex_lock(&sStackLock);
{
ScopedPthreadLock lock(&sStackLock);

// Make sure our buffer is big enough, but this will need a better setup!
static const size_t bufferSize = 1024;
auto * buffer = System::PacketBuffer::NewWithAvailableSize(bufferSize);
// Make sure our buffer is big enough, but this will need a better setup!
static const size_t bufferSize = 1024;
auto * buffer = System::PacketBuffer::NewWithAvailableSize(bufferSize);

// Hardcode endpoint to 1 for now
uint8_t endpoint = 1;
if (buffer == nullptr)
{
err = CHIP_ERROR_NO_MEMORY;
}
else
{

uint16_t dataLength = 0;
switch (commandID)
{
case 0:
dataLength = encodeOffCommand(buffer->Start(), bufferSize, endpoint);
break;
case 1:
dataLength = encodeOnCommand(buffer->Start(), bufferSize, endpoint);
break;
case 2:
dataLength = encodeToggleCommand(buffer->Start(), bufferSize, endpoint);
break;
default:
ChipLogError(Controller, "Unknown command: %d", commandID);
return;
}
// Hardcode endpoint to 1 for now
uint8_t endpoint = 1;

buffer->SetDataLength(dataLength);
uint16_t dataLength = 0;
switch (commandID)
{
case 0:
dataLength = encodeOffCommand(buffer->Start(), bufferSize, endpoint);
break;
case 1:
dataLength = encodeOnCommand(buffer->Start(), bufferSize, endpoint);
break;
case 2:
dataLength = encodeToggleCommand(buffer->Start(), bufferSize, endpoint);
break;
default:
ChipLogError(Controller, "Unknown command: %d", commandID);
return;
}

// Hardcode endpoint to 1 for now
err = deviceController->SendMessage((void *) "SendMessage", buffer);
buffer->SetDataLength(dataLength);

pthread_mutex_unlock(&sStackLock);
// Hardcode endpoint to 1 for now
err = deviceController->SendMessage((void *) "SendMessage", buffer);
}
}

if (err != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -384,9 +418,10 @@ JNI_METHOD(jboolean, disconnectDevice)(JNIEnv * env, jobject self, jlong deviceC

ChipDeviceController * deviceController = (ChipDeviceController *) deviceControllerPtr;

pthread_mutex_lock(&sStackLock);
err = deviceController->DisconnectDevice();
pthread_mutex_unlock(&sStackLock);
{
ScopedPthreadLock lock(&sStackLock);
err = deviceController->DisconnectDevice();
}

if (err != CHIP_NO_ERROR)
{
Expand Down
21 changes: 14 additions & 7 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,14 @@ - (BOOL)sendMessage:(NSData *)message error:(NSError * __autoreleasing *)error
const void * messageChars = [message bytes];

chip::System::PacketBuffer * buffer = chip::System::PacketBuffer::NewWithAvailableSize(messageLen);
buffer->SetDataLength(messageLen);
if (!buffer) {
err = CHIP_ERROR_NO_MEMORY;
} else {
buffer->SetDataLength(messageLen);

memcpy(buffer->Start(), messageChars, messageLen);
err = self.cppController->SendMessage((__bridge void *) self, buffer);
memcpy(buffer->Start(), messageChars, messageLen);
err = self.cppController->SendMessage((__bridge void *) self, buffer);
}
[self.lock unlock];

if (err != CHIP_NO_ERROR) {
Expand All @@ -312,11 +316,14 @@ - (BOOL)sendCHIPCommand:(uint32_t (^)(chip::System::PacketBuffer *, uint16_t))en
// FIXME: This needs a better buffersizing setup!
static const size_t bufferSize = 1024;
chip::System::PacketBuffer * buffer = chip::System::PacketBuffer::NewWithAvailableSize(bufferSize);
if (!buffer) {
err = CHIP_ERROR_NO_MEMORY;
} else {
uint32_t dataLength = encodeCommandBlock(buffer, (uint16_t) bufferSize);
buffer->SetDataLength(dataLength);

uint32_t dataLength = encodeCommandBlock(buffer, (uint16_t) bufferSize);
buffer->SetDataLength(dataLength);

err = self.cppController->SendMessage((__bridge void *) self, buffer);
err = self.cppController->SendMessage((__bridge void *) self, buffer);
}
[self.lock unlock];
if (err != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Error(%d): %@, send failed", err, [CHIPError errorForCHIPErrorCode:err]);
Expand Down
2 changes: 2 additions & 0 deletions src/transport/RendezvousSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ CHIP_ERROR RendezvousSession::HandleSecureMessage(PacketBuffer * msgBuf)
allocated as an inline buffer to PacketBuffer structure */
origMsg = msgBuf;
msgBuf = PacketBuffer::NewWithAvailableSize(len);
VerifyOrExit(msgBuf != nullptr, err = CHIP_ERROR_NO_MEMORY);

msgBuf->SetDataLength(len, msgBuf);
#endif
plainText = msgBuf->Start();
Expand Down
1 change: 1 addition & 0 deletions src/transport/SecureSessionMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ void SecureSessionMgrBase::HandleDataReceived(const PacketHeader & packetHeader,
allocated as an inline buffer to PacketBuffer structure */
origMsg = msg;
msg = PacketBuffer::NewWithAvailableSize(len);
VerifyOrExit(msg != nullptr, ChipLogError(Inet, "Insufficient memory for packet buffer."));
msg->SetDataLength(len, msg);
#endif
plainText = msg->Start();
Expand Down
2 changes: 2 additions & 0 deletions src/transport/raw/tests/TestTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ void CheckMessageTest(nlTestSuite * inSuite, void * inContext, const IPAddress &
uint16_t payload_len = sizeof(PAYLOAD);

chip::System::PacketBuffer * buffer = chip::System::PacketBuffer::NewWithAvailableSize(payload_len);
NL_TEST_ASSERT(inSuite, buffer != nullptr);

memmove(buffer->Start(), PAYLOAD, payload_len);
buffer->SetDataLength(payload_len);

Expand Down
2 changes: 2 additions & 0 deletions src/transport/raw/tests/TestUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void CheckMessageTest(nlTestSuite * inSuite, void * inContext, const IPAddress &
uint16_t payload_len = sizeof(PAYLOAD);

chip::System::PacketBuffer * buffer = chip::System::PacketBuffer::NewWithAvailableSize(payload_len);
NL_TEST_ASSERT(inSuite, buffer != nullptr);

memmove(buffer->Start(), PAYLOAD, payload_len);
buffer->SetDataLength(payload_len);

Expand Down
2 changes: 2 additions & 0 deletions src/transport/tests/TestSecureSessionMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ void CheckMessageTest(nlTestSuite * inSuite, void * inContext)
ctx.GetInetLayer().SystemLayer()->Init(nullptr);

chip::System::PacketBuffer * buffer = chip::System::PacketBuffer::NewWithAvailableSize(payload_len);
NL_TEST_ASSERT(inSuite, buffer != nullptr);

memmove(buffer->Start(), PAYLOAD, payload_len);
buffer->SetDataLength(payload_len);

Expand Down