system/uorb: fix memory leak report by coverity

nsh> uorb_unit_test
uORB note: try single-topic support
uORB note: PASS single-topic test
uORB note: PASS orb 10-instances
uORB note: try multi-topic support
uORB note: ---------------- LATENCY TEST ------------------
mean:    0 us
std dev: 0 us
min:     0 us
max:     0 us
missed topic updates: 0
uORB note: PASS multi-topic test
uORB note: try multi-topic support subscribing before publishing
uORB note: PASS multi-topic reversed
uORB note: Testing unadvertise
uORB note: Testing multi-topic 2 test (queue simulation)
uORB note: PASS multi-topic 2 test (queue simulation)
uORB note: Testing orb queuing
uORB note:   Testing to write some elements...
uORB note:   Testing overflow...
uORB note:   Testing underflow...
uORB note: PASS orb queuing
uORB note: Testing orb queuing (poll & notify)
PASS

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1
2024-04-21 15:04:31 +08:00
committed by Xiang Xiao
parent 5b729162ad
commit 5732c76f39

View File

@@ -194,6 +194,7 @@ static int latency_test(bool print)
struct orb_test_medium_s sample; struct orb_test_medium_s sample;
pthread_t pubsub_task; pthread_t pubsub_task;
int instance = 0; int instance = 0;
int ret = ERROR;
int fd; int fd;
test_note("---------------- LATENCY TEST ------------------"); test_note("---------------- LATENCY TEST ------------------");
@@ -213,7 +214,8 @@ static int latency_test(bool print)
if (pthread_create(&pubsub_task, NULL, pubsubtest_thread_entry, NULL) < 0) if (pthread_create(&pubsub_task, NULL, pubsubtest_thread_entry, NULL) < 0)
{ {
return test_fail("failed launching task"); test_fail("failed launching task");
goto out;
} }
/* give the test task some data */ /* give the test task some data */
@@ -224,7 +226,8 @@ static int latency_test(bool print)
sample.timestamp = orb_absolute_time(); sample.timestamp = orb_absolute_time();
if (OK != orb_publish(ORB_ID(orb_test_medium), fd, &sample)) if (OK != orb_publish(ORB_ID(orb_test_medium), fd, &sample))
{ {
return test_fail("mult. pub0 timing fail"); test_fail("mult. pub0 timing fail");
goto out;
} }
usleep(1000); /* simulate >800 Hz system operation */ usleep(1000); /* simulate >800 Hz system operation */
@@ -232,11 +235,15 @@ static int latency_test(bool print)
if (pubsub_task < 0) if (pubsub_task < 0)
{ {
return test_fail("failed launching task"); test_fail("failed launching task");
goto out;
} }
ret = OK;
out:
orb_unadvertise(fd); orb_unadvertise(fd);
return g_pubsubtest_res; return ret < 0 ? ret : g_pubsubtest_res;
} }
static int test_single(void) static int test_single(void)
@@ -247,7 +254,7 @@ static int test_single(void)
bool updated; bool updated;
int afd; int afd;
int sfd; int sfd;
int ret; int ret = ERROR;
test_note("try single-topic support"); test_note("try single-topic support");
@@ -265,6 +272,7 @@ static int test_single(void)
sfd = orb_subscribe(ORB_ID(orb_test)); sfd = orb_subscribe(ORB_ID(orb_test));
if (sfd < 0) if (sfd < 0)
{ {
orb_unadvertise(afd);
return test_fail("subscribe failed: %d", errno); return test_fail("subscribe failed: %d", errno);
} }
@@ -274,23 +282,27 @@ static int test_single(void)
if (OK != orb_copy(ORB_ID(orb_test), sfd, &sub_sample)) if (OK != orb_copy(ORB_ID(orb_test), sfd, &sub_sample))
{ {
return test_fail("copy(1) failed: %d", errno); test_fail("copy(1) failed: %d", errno);
goto out;
} }
if (sample.val != sub_sample.val) if (sample.val != sub_sample.val)
{ {
return test_fail("copy(1) mismatch: %d expected %d", test_fail("copy(1) mismatch: %d expected %d",
sub_sample.val, sample.val); sub_sample.val, sample.val);
goto out;
} }
if (OK != orb_check(sfd, &updated)) if (OK != orb_check(sfd, &updated))
{ {
return test_fail("check(1) failed"); test_fail("check(1) failed");
goto out;
} }
if (updated) if (updated)
{ {
return test_fail("spurious updated flag"); test_fail("spurious updated flag");
goto out;
} }
/* check second publish */ /* check second publish */
@@ -299,45 +311,56 @@ static int test_single(void)
if (OK != orb_publish(ORB_ID(orb_test), afd, &sample)) if (OK != orb_publish(ORB_ID(orb_test), afd, &sample))
{ {
return test_fail("publish failed"); test_fail("publish failed");
goto out;
} }
if (OK != orb_check(sfd, &updated)) if (OK != orb_check(sfd, &updated))
{ {
return test_fail("check(2) failed"); test_fail("check(2) failed");
goto out;
} }
if (!updated) if (!updated)
{ {
return test_fail("missing updated flag"); test_fail("missing updated flag");
goto out;
} }
if (OK != orb_copy(ORB_ID(orb_test), sfd, &sub_sample)) if (OK != orb_copy(ORB_ID(orb_test), sfd, &sub_sample))
{ {
return test_fail("copy(2) failed: %d", errno); test_fail("copy(2) failed: %d", errno);
goto out;
} }
if (sample.val != sub_sample.val) if (sample.val != sub_sample.val)
{ {
return test_fail("copy(2) mismatch: %d expected %d", test_fail("copy(2) mismatch: %d expected %d",
sub_sample.val, sample.val); sub_sample.val, sample.val);
goto out;
} }
/* unadvertise and out */ /* unadvertise and out */
out:
ret = orb_unadvertise(afd); ret = orb_unadvertise(afd);
if (ret != OK) if (ret != OK)
{ {
return test_fail("orb_unadvertise failed: %i", ret); test_fail("orb_unadvertise failed: %i", ret);
} }
ret = orb_unsubscribe(sfd); ret = orb_unsubscribe(sfd);
if (ret != OK) if (ret != OK)
{ {
return test_fail("orb_unsubscribe failed: %i", ret); test_fail("orb_unsubscribe failed: %i", ret);
} }
return test_note("PASS single-topic test"); if (ret == OK)
{
test_note("PASS single-topic test");
}
return ret;
} }
static int test_multi_inst10(void) static int test_multi_inst10(void)
@@ -345,6 +368,7 @@ static int test_multi_inst10(void)
const int max_inst = 10; const int max_inst = 10;
int sfd[max_inst]; int sfd[max_inst];
int afd[max_inst]; int afd[max_inst];
int ret = ERROR;
int i; int i;
int j; int j;
@@ -359,7 +383,8 @@ static int test_multi_inst10(void)
{ {
if (OK == orb_exists(ORB_ID(orb_test), i)) if (OK == orb_exists(ORB_ID(orb_test), i))
{ {
return test_fail("sub %d is advertised", i); test_fail("sub %d is advertised", i);
goto out_with_sfd;
} }
} }
@@ -369,9 +394,16 @@ static int test_multi_inst10(void)
{ {
afd[i] = orb_advertise_multi_queue_persist(ORB_ID(orb_test), afd[i] = orb_advertise_multi_queue_persist(ORB_ID(orb_test),
NULL, &i, 1); NULL, &i, 1);
}
/* verify advertised yet */
for (i = 0; i < max_inst; i++)
{
if (OK != orb_exists(ORB_ID(orb_test), i)) if (OK != orb_exists(ORB_ID(orb_test), i))
{ {
return test_fail("sub %d advertise failed", i); test_fail("sub %d advertise failed", i);
goto out_with_sfd;
} }
} }
@@ -401,7 +433,8 @@ static int test_multi_inst10(void)
orb_check(sfd[sub_instance], &updated); orb_check(sfd[sub_instance], &updated);
if (!updated) if (!updated)
{ {
return test_fail("sub %d not updated", sub_instance); test_fail("sub %d not updated", sub_instance);
goto out;
} }
else else
{ {
@@ -409,14 +442,16 @@ static int test_multi_inst10(void)
if (orb_copy(ORB_ID(orb_test), sfd[sub_instance], &sample)) if (orb_copy(ORB_ID(orb_test), sfd[sub_instance], &sample))
{ {
return test_fail("sub %d copy failed", sub_instance); test_fail("sub %d copy failed", sub_instance);
goto out;
} }
else else
{ {
if (sample.val != (j * sub_instance + i)) if (sample.val != (j * sub_instance + i))
{ {
return test_fail("sub %d invalid value %d", test_fail("sub %d invalid value %d",
sub_instance, sample.val); sub_instance, sample.val);
goto out;
} }
} }
} }
@@ -424,14 +459,26 @@ static int test_multi_inst10(void)
} }
} }
/* force unsubscribe all, then repeat */ ret = OK;
out:
for (i = 0; i < max_inst; i++)
{
orb_unadvertise(afd[i]);
}
out_with_sfd:
for (i = 0; i < max_inst; i++) for (i = 0; i < max_inst; i++)
{ {
orb_unsubscribe(sfd[i]); orb_unsubscribe(sfd[i]);
} }
return test_note("PASS orb 10-instances"); if (ret == OK)
{
test_note("PASS orb 10-instances");
}
return ret;
} }
static int test_multi(int *afds, int *sfds) static int test_multi(int *afds, int *sfds)
@@ -671,6 +718,7 @@ static int test_multi2(void)
struct sched_param param; struct sched_param param;
pthread_attr_t attr; pthread_attr_t attr;
pthread_t pubsub_task; pthread_t pubsub_task;
int ret = ERROR;
int i; int i;
test_note("Testing multi-topic 2 test (queue simulation)"); test_note("Testing multi-topic 2 test (queue simulation)");
@@ -693,7 +741,8 @@ static int test_multi2(void)
if (pthread_create(&pubsub_task, &attr, pub_test_multi2_entry, NULL) < 0) if (pthread_create(&pubsub_task, &attr, pub_test_multi2_entry, NULL) < 0)
{ {
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
return test_fail("failed launching task"); test_fail("failed launching task");
goto out;
} }
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
@@ -718,13 +767,15 @@ static int test_multi2(void)
if (OK != orb_copy(ORB_ID(orb_test_medium_multi), if (OK != orb_copy(ORB_ID(orb_test_medium_multi),
orb_data_cur_fd, &msg)) orb_data_cur_fd, &msg))
{ {
return test_fail("copy failed: %d", errno); test_fail("copy failed: %d", errno);
goto out;
} }
if (last_time >= msg.timestamp && last_time != 0) if (last_time >= msg.timestamp && last_time != 0)
{ {
return test_fail("Timestamp not increasing! (%" PRIu64 test_fail("Timestamp not increasing! (%" PRIu64
" >= %" PRIu64 ")", last_time, msg.timestamp); " >= %" PRIu64 ")", last_time, msg.timestamp);
goto out;
} }
last_time = msg.timestamp; last_time = msg.timestamp;
@@ -732,12 +783,20 @@ static int test_multi2(void)
} }
} }
ret = OK;
out:
for (i = 0; i < num_instances; ++i) for (i = 0; i < num_instances; ++i)
{ {
orb_unsubscribe(orb_data_fd[i]); orb_unsubscribe(orb_data_fd[i]);
} }
return test_note("PASS multi-topic 2 test (queue simulation)"); if (ret == OK)
{
test_note("PASS multi-topic 2 test (queue simulation)");
}
return ret;
} }
int test_queue(void) int test_queue(void)
@@ -750,6 +809,7 @@ int test_queue(void)
int instance = 0; int instance = 0;
int ptopic; int ptopic;
int sfd; int sfd;
int ret = ERROR;
int i; int i;
test_note("Testing orb queuing"); test_note("Testing orb queuing");
@@ -779,52 +839,60 @@ int test_queue(void)
ORB_ID(orb_test_medium_queue), &sample, &instance, queue_size); ORB_ID(orb_test_medium_queue), &sample, &instance, queue_size);
if (ptopic < 0) if (ptopic < 0)
{ {
orb_unsubscribe(sfd);
return test_fail("advertise failed: %d", errno); return test_fail("advertise failed: %d", errno);
} }
orb_check(sfd, &updated); orb_check(sfd, &updated);
if (!updated) if (!updated)
{ {
return test_fail("update flag not set"); test_fail("update flag not set");
goto out;
} }
if (OK != orb_copy(ORB_ID(orb_test_medium_queue), sfd, &sub_sample)) if (OK != orb_copy(ORB_ID(orb_test_medium_queue), sfd, &sub_sample))
{ {
return test_fail("copy(1) failed: %d", errno); test_fail("copy(1) failed: %d", errno);
goto out;
} }
if (sub_sample.val != sample.val) if (sub_sample.val != sample.val)
{ {
return test_fail("copy(1) mismatch: %d expected %d", test_fail("copy(1) mismatch: %d expected %d",
sub_sample.val, sample.val); sub_sample.val, sample.val);
goto out;
} }
orb_check(sfd, &updated); orb_check(sfd, &updated);
if (updated) if (updated)
{ {
return test_fail("spurious updated flag"); test_fail("spurious updated flag");
goto out;
} }
#define CHECK_UPDATED(element) \ #define CHECK_UPDATED(element) \
orb_check(sfd, &updated); \ orb_check(sfd, &updated); \
if (!updated) \ if (!updated) \
{ \ { \
return test_fail("update flag not set, element %i", element); \ test_fail("update flag not set, element %i", element); \
goto out; \
} }
#define CHECK_NOT_UPDATED(element) \ #define CHECK_NOT_UPDATED(element) \
orb_check(sfd, &updated); \ orb_check(sfd, &updated); \
if (updated) \ if (updated) \
{ \ { \
return test_fail("update flag set, element %i", element); \ test_fail("update flag set, element %i", element); \
goto out; \
} }
#define CHECK_COPY(i_got, i_correct) \ #define CHECK_COPY(i_got, i_correct) \
orb_copy(ORB_ID(orb_test_medium_queue), sfd, &sub_sample); \ orb_copy(ORB_ID(orb_test_medium_queue), sfd, &sub_sample); \
if (i_got != i_correct) \ if (i_got != i_correct) \
{ \ { \
return test_fail("got wrong element from the queue (got %i," \ test_fail("got wrong element from the queue (got %i," \
"should be %i)", i_got, i_correct); \ "should be %i)", i_got, i_correct); \
goto out; \
} }
/* no messages in the queue anymore */ /* no messages in the queue anymore */
@@ -880,10 +948,18 @@ int test_queue(void)
#undef CHECK_UPDATED #undef CHECK_UPDATED
#undef CHECK_NOT_UPDATED #undef CHECK_NOT_UPDATED
ret = OK;
out:
orb_unadvertise(ptopic); orb_unadvertise(ptopic);
orb_unsubscribe(sfd); orb_unsubscribe(sfd);
return test_note("PASS orb queuing"); if (ret == OK)
{
test_note("PASS orb queuing");
}
return ret;
} }
static FAR void *pub_test_queue_entry(FAR void *arg) static FAR void *pub_test_queue_entry(FAR void *arg)
@@ -973,6 +1049,7 @@ static int test_queue_poll_notify(void)
if (pthread_create(&pubsub_task, &attr, pub_test_queue_entry, NULL) < 0) if (pthread_create(&pubsub_task, &attr, pub_test_queue_entry, NULL) < 0)
{ {
orb_unsubscribe(sfd);
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
return test_fail("failed test queue task"); return test_fail("failed test queue task");
} }
@@ -994,10 +1071,12 @@ static int test_queue_poll_notify(void)
break; break;
} }
orb_unsubscribe(sfd);
return test_fail("poll timeout"); return test_fail("poll timeout");
} }
else if (poll_ret < 0 && errno != EINTR) else if (poll_ret < 0 && errno != EINTR)
{ {
orb_unsubscribe(sfd);
return test_fail("poll error (%d, %d)", poll_ret, errno); return test_fail("poll error (%d, %d)", poll_ret, errno);
} }
@@ -1006,6 +1085,7 @@ static int test_queue_poll_notify(void)
orb_copy(ORB_ID(orb_test_medium_queue_poll), sfd, &t); orb_copy(ORB_ID(orb_test_medium_queue_poll), sfd, &t);
if (next_expected_val != t.val) if (next_expected_val != t.val)
{ {
orb_unsubscribe(sfd);
return test_fail("copy mismatch: %d expected %d", return test_fail("copy mismatch: %d expected %d",
t.val, next_expected_val); t.val, next_expected_val);
} }
@@ -1016,6 +1096,7 @@ static int test_queue_poll_notify(void)
if (g_num_messages_sent != next_expected_val) if (g_num_messages_sent != next_expected_val)
{ {
orb_unsubscribe(sfd);
return test_fail("number of sent and received messages mismatch" return test_fail("number of sent and received messages mismatch"
" (sent: %i, received: %i)", " (sent: %i, received: %i)",
g_num_messages_sent, next_expected_val); g_num_messages_sent, next_expected_val);