Skip to content

Commit

Permalink
Fix crash when calling nastConnection_Connect() with NULL options.
Browse files Browse the repository at this point in the history
When called with a NULL options, the call will use default options, in effect, the call is then equivalent to natsConnection_ConnectTo(&nc, NATS_DEFAULT_URL);
  • Loading branch information
kozlovic committed Nov 12, 2015
1 parent 5ee3b6d commit 2258463
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,9 @@ natsConnection_Connect(natsConnection **newConn, natsOptions *options)
natsConnection *nc = NULL;
natsOptions *opts = NULL;

if (options == NULL)
return natsConnection_ConnectTo(newConn, NATS_DEFAULT_URL);

opts = natsOptions_clone(options);
if (opts == NULL)
return NATS_NO_MEMORY;
Expand Down
1 change: 1 addition & 0 deletions test/list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ServersRandomize
SelectNextServer
DefaultConnection
UseDefaultURLIfNoServerSpecified
ConnectionWithNULLOptions
ConnectionStatus
ConnClosedCB
CloseDisconnectedCB
Expand Down
21 changes: 21 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2926,6 +2926,26 @@ test_UseDefaultURLIfNoServerSpecified(void)
_stopServer(serverPid);
}

static void
test_ConnectionWithNullOptions(void)
{
natsStatus s;
natsConnection *nc = NULL;
natsPid serverPid = NATS_INVALID_PID;

serverPid = _startServer(NATS_DEFAULT_URL, NULL, true);
if (serverPid == NATS_INVALID_PID)
FAIL("Unable to start or verify that the server was started!");

test("Check connect with NULL options is allowed: ");
s = natsConnection_Connect(&nc, NULL);
testCond(s == NATS_OK);

natsConnection_Destroy(nc);

_stopServer(serverPid);
}

static void
test_ConnectionStatus(void)
{
Expand Down Expand Up @@ -6871,6 +6891,7 @@ static testInfo allTests[] =

{"DefaultConnection", test_DefaultConnection},
{"UseDefaultURLIfNoServerSpecified",test_UseDefaultURLIfNoServerSpecified},
{"ConnectionWithNULLOptions", test_ConnectionWithNullOptions},
{"ConnectionStatus", test_ConnectionStatus},
{"ConnClosedCB", test_ConnClosedCB},
{"CloseDisconnectedCB", test_CloseDisconnectedCB},
Expand Down

0 comments on commit 2258463

Please sign in to comment.