NetBSD Problem Report #57478

From www@netbsd.org  Thu Jun 22 03:48:47 2023
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 92ABB1A923E
	for <gnats-bugs@gnats.NetBSD.org>; Thu, 22 Jun 2023 03:48:47 +0000 (UTC)
Message-Id: <20230622034846.227A01A9241@mollari.NetBSD.org>
Date: Thu, 22 Jun 2023 03:48:46 +0000 (UTC)
From: rin@iij.ad.jp
Reply-To: rin@iij.ad.jp
To: gnats-bugs@NetBSD.org
Subject: ntohs(3) and friends behave differently to manpage for big endian
X-Send-Pr-Version: www-1.0

>Number:         57478
>Category:       lib
>Synopsis:       ntohs(3) and friends behave differently to manpage for big endian
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jun 22 03:50:00 +0000 2023
>Originator:     Rin Okuyama
>Release:        10.99.3
>Organization:
Internet Initiative Japan Inc.
>Environment:
NetBSD console 10.99.3 NetBSD 10.99.3 (SUNXI_NET_MPSAFE) #0: Mon May 22 11:10:46 JST 2023  rin@latipes:/build/src/sys/arch/evbarm/compile/SUNXI_NET_MPSAFE evbarm earmv7hfeb
>Description:
ntohs(3) reads:

----
     uint16_t
     ntohs(uint16_t net16);
----

However, it is defined for big endian machines as:

<sys/endian.h>:
----
...
   112  #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
   113  #define ntohl(x)        (x)
   114  #define ntohs(x)        (x)
   115  #define htonl(x)        (x)
   116  #define htons(x)        (x)
...
----

Therefore, ntohs(0x11111111) yeilds 0x11111111 on big endian machines.

This behavior is apparently inconsistent with its manpage (and
POSIX). However, this should be a REALLY old bug, and I'm not 100%
sure whether it is safe to correct this.
>How-To-Repeat:
Compile and run this sample code:

----
#include <sys/endian.h>

#include <stdio.h>

int
main(void)
{
	int i;

	i = ntohs(0x11111111);
	printf("0x%08x\n", i);
	return 0;
}
----

0x00001111 and 0x11111111 are obtained for amd64 and earmv7hf-eb,
respectively.
>Fix:
#define ntohs(x) ((uint16_t)(x)) and so on.

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.