From f351d62355b57e5d3905c076b85777e00512435f Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 2 Sep 2016 19:57:25 +0300 Subject: [PATCH] tests/jid: fixed crash on QNX QNX version of printf() doesn't check whether a string is valid or not. Passing NULL pointer leads to a crash inside strlen(). Fix this with a wrapper _s() which returns "" for instead of NULL pointer. Add checks before strcmp() to avoid crash in case of unexpected result of the jid functions. --- tests/test_jid.c | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/tests/test_jid.c b/tests/test_jid.c index f882a2d..97af8be 100644 --- a/tests/test_jid.c +++ b/tests/test_jid.c @@ -1,7 +1,7 @@ /* test_jid.c ** libstrophe XMPP client library -- test routines for the jid utils ** -** Copyright (C) 2005-2009 Collecta, Inc. +** Copyright (C) 2005-2009 Collecta, Inc. ** ** This software is provided AS-IS with no warranty, either express ** or implied. @@ -20,6 +20,11 @@ static const char jid2[] = "anyone@example.com/hullo"; static const char jid3[] = "manic.porter@xyz.net/frob"; static const char jid4[] = "domain.tld"; +static const char *_s(const char *s) +{ + return s == NULL ? "" : s; +} + int test_jid(xmpp_ctx_t *ctx) { char *node; @@ -30,9 +35,9 @@ int test_jid(xmpp_ctx_t *ctx) domain = xmpp_jid_domain(ctx, jid1); resource = xmpp_jid_resource(ctx, jid1); printf("jid '%s' parsed to %s, %s, %s\n", - jid1, node, domain, resource); - if (strcmp(node, "foo")) return 1; - if (strcmp(domain, "bar.com")) return 1; + jid1, _s(node), _s(domain), _s(resource)); + if (node == NULL || strcmp(node, "foo")) return 1; + if (domain == NULL || strcmp(domain, "bar.com")) return 1; if (resource != NULL) return 1; if (node) xmpp_free(ctx, node); if (domain) xmpp_free(ctx, domain); @@ -42,10 +47,10 @@ int test_jid(xmpp_ctx_t *ctx) domain = xmpp_jid_domain(ctx, jid2); resource = xmpp_jid_resource(ctx, jid2); printf("jid '%s' parsed to %s, %s, %s\n", - jid2, node, domain, resource); - if (strcmp(node, "anyone")) return 1; - if (strcmp(domain, "example.com")) return 1; - if (strcmp(resource, "hullo")) return 1; + jid2, _s(node), _s(domain), _s(resource)); + if (node == NULL || strcmp(node, "anyone")) return 1; + if (domain == NULL || strcmp(domain, "example.com")) return 1; + if (resource == NULL || strcmp(resource, "hullo")) return 1; if (node) xmpp_free(ctx, node); if (domain) xmpp_free(ctx, domain); if (resource) xmpp_free(ctx, resource); @@ -54,10 +59,10 @@ int test_jid(xmpp_ctx_t *ctx) domain = xmpp_jid_domain(ctx, jid3); resource = xmpp_jid_resource(ctx, jid3); printf("jid '%s' parsed to %s, %s, %s\n", - jid3, node, domain, resource); - if (strcmp(node, "manic.porter")) return 1; - if (strcmp(domain, "xyz.net")) return 1; - if (strcmp(resource, "frob")) return 1; + jid3, _s(node), _s(domain), _s(resource)); + if (node == NULL || strcmp(node, "manic.porter")) return 1; + if (domain == NULL || strcmp(domain, "xyz.net")) return 1; + if (resource == NULL || strcmp(resource, "frob")) return 1; if (node) xmpp_free(ctx, node); if (domain) xmpp_free(ctx, domain); if (resource) xmpp_free(ctx, resource); @@ -66,9 +71,9 @@ int test_jid(xmpp_ctx_t *ctx) domain = xmpp_jid_domain(ctx, jid4); resource = xmpp_jid_resource(ctx, jid4); printf("jid '%s' parsed to %s, %s, %s\n", - jid4, node, domain, resource); + jid4, _s(node), _s(domain), _s(resource)); if (node != NULL) return 1; - if (strcmp(domain, "domain.tld")) return 1; + if (domain == NULL || strcmp(domain, "domain.tld")) return 1; if (resource != NULL) return 1; if (node) xmpp_free(ctx, node); if (domain) xmpp_free(ctx, domain); @@ -99,23 +104,23 @@ int main(int argc, char *argv[]) xmpp_ctx_t *ctx; int ret; - printf("allocating context... "); + printf("allocating context... "); ctx = xmpp_ctx_new(NULL, NULL); if (ctx == NULL) printf("failed to create context\n"); if (ctx == NULL) return -1; printf("ok.\n"); - printf("testing jid routines... "); + printf("testing jid routines...\n"); ret = test_jid(ctx); - if (ret) printf("failed!\n"); + if (ret) printf("testing jid routines... failed!\n"); if (ret) return ret; - printf("ok.\n"); + printf("testing jid routines... ok.\n"); - printf("testing jid new routines... "); + printf("testing jid new routines...\n"); ret = test_jid_new(ctx); - if (ret) printf("failed!\n"); + if (ret) printf("testing jid new routines... failed!\n"); if (ret) return ret; - printf("ok.\n"); + printf("testing jid new routines... ok.\n"); printf("freeing context... "); xmpp_ctx_free(ctx);