Disabling IPV6 causes ***** USAGE FAULT ***** in mqtt_helper for getaddrinfo

I just ported your AWS IoT for MQTT from this repository.

If I keep IPv6 enabled, I can get the address information without any problem. However, if I disable IPv6, the result is not null, but the application crashes at if (addr->ai_family == AF_INET6) and gets stuck. So any opinion to work aroud.

Zephyr: 

revision: v4.0-branch
Nrf: 
revision: v2.9-branch

 

[00:00:57.088,000] <err> os: ***** USAGE FAULT *****
[00:00:57.088,000] <err> os:   Unaligned memory access
[00:00:57.088,000] <err> os: r0/a1:  0x00000011  r1/a2:  0x00000000  r2/a3:  0x00000012
[00:00:57.088,000] <err> os: r3/a4:  0x20007fa0 r12/ip:  0x00000317 r14/lr:  0x080567d1
[00:00:57.088,000] <err> os:  xpsr:  0x21000000
[00:00:57.088,000] <err> os: s[ 0]:  0x00000301  s[ 1]:  0x00000010  s[ 2]:  0x00000000  s[ 3]:  0x2008d5f4
[00:00:57.088,000] <err> os: s[ 4]:  0x00000000  s[ 5]:  0x080ab79b  s[ 6]:  0x00000000  s[ 7]:  0x00000000
[00:00:57.088,000] <err> os: s[ 8]:  0x00000000  s[ 9]:  0x44554b2b  s[10]:  0x43525f50  s[11]:  0x22203a56
[00:00:57.088,000] <err> os: s[12]:  0x312e3031  s[13]:  0x312e3530  s[14]:  0x35322e36  s[15]:  0x352c2234
[00:00:57.088,000] <err> os: fpscr:  0x00000033
[00:00:57.088,000] <err> os: Faulting instruction address (r15/pc): 0x080567d0
[00:00:57.088,000] <err> os: >>> ZEPHYR FATAL ERROR 31: Unknown error on CPU 0
[00:00:57.088,000] <err> os: Current thread: 0x20013218 (sysworkq)
[00:00:57.329,000] <err> os: Halting system

static int broker_init(struct sockaddr_storage *broker,
		       struct mqtt_helper_conn_params *conn_params)
{
	int err;
	struct addrinfo *result;
	struct addrinfo *addr;
	struct addrinfo hints = {
		.ai_socktype = SOCK_STREAM
	};
	char addr_str[NET_IPV6_ADDR_LEN];
	printk("%d %s\n", __LINE__, __func__);

	if (sizeof(CONFIG_MQTT_HELPER_STATIC_IP_ADDRESS) > 1) {
		conn_params->hostname.ptr = CONFIG_MQTT_HELPER_STATIC_IP_ADDRESS;

		LOG_DBG("Using static IP address: %s", CONFIG_MQTT_HELPER_STATIC_IP_ADDRESS);
	} else {
		LOG_DBG("Resolving IP address for %s", conn_params->hostname.ptr);
	}

	err = getaddrinfo(conn_params->hostname.ptr, NULL, &hints, &result);
	if (err) {
		LOG_ERR("getaddrinfo() failed, error %d", err);
		return -err;
	}
	printk("%d %s\n", __LINE__, __func__);
	addr = result;
	printk("%d %s\n", __LINE__, __func__);

	while (addr != NULL) {
			printk("%d %s\n", __LINE__, __func__);

		if (addr->ai_family == AF_INET6) {
				printk("%d %s\n", __LINE__, __func__);

			struct sockaddr_in6 *broker6 = ((struct sockaddr_in6 *)broker);

			net_ipaddr_copy(&broker6->sin6_addr,
					&((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
			broker6->sin6_family = addr->ai_family;
			broker6->sin6_port = htons(CONFIG_MQTT_HELPER_PORT);

			inet_ntop(addr->ai_family, &broker6->sin6_addr, addr_str,
				sizeof(addr_str));
			LOG_DBG("IPv6 Address found %s (%s)", addr_str,
				net_family2str(addr->ai_family));
			break;
		} else if (addr->ai_family == AF_INET) {
				printk("%d %s\n", __LINE__, __func__);

			struct sockaddr_in *broker4 = ((struct sockaddr_in *)broker);

			net_ipaddr_copy(&broker4->sin_addr,
					&((struct sockaddr_in *)addr->ai_addr)->sin_addr);
			broker4->sin_family = addr->ai_family;
			broker4->sin_port = htons(CONFIG_MQTT_HELPER_PORT);

			inet_ntop(addr->ai_family, &broker4->sin_addr, addr_str,
				sizeof(addr_str));
			LOG_DBG("IPv4 Address found %s (%s)", addr_str,
				net_family2str(addr->ai_family));
			break;
		} else {
			LOG_DBG("Unknown address family %d", (unsigned int)addr->ai_family);
		}
			printk("%d %s\n", __LINE__, __func__);

		addr = addr->ai_next;
	}

	freeaddrinfo(result);

	return err;
}

Related