From owner-freebsd-standards@FreeBSD.ORG Mon Aug 6 05:50:08 2012 Return-Path: Delivered-To: freebsd-standards@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 44F041065678 for ; Mon, 6 Aug 2012 05:50:08 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id BEB8F8FC0A for ; Mon, 6 Aug 2012 05:50:07 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.5/8.14.5) with ESMTP id q765o1n0058129 for ; Mon, 6 Aug 2012 05:50:01 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.5/8.14.5/Submit) id q765o1jh058128; Mon, 6 Aug 2012 05:50:01 GMT (envelope-from gnats) Resent-Date: Mon, 6 Aug 2012 05:50:01 GMT Resent-Message-Id: <201208060550.q765o1jh058128@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-standards@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, William Ahern Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 72AC91065670 for ; Mon, 6 Aug 2012 05:41:48 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22]) by mx1.freebsd.org (Postfix) with ESMTP id 5D96F8FC0C for ; Mon, 6 Aug 2012 05:41:48 +0000 (UTC) Received: from red.freebsd.org (localhost [127.0.0.1]) by red.freebsd.org (8.14.4/8.14.4) with ESMTP id q765flLv012686 for ; Mon, 6 Aug 2012 05:41:47 GMT (envelope-from nobody@red.freebsd.org) Received: (from nobody@localhost) by red.freebsd.org (8.14.4/8.14.4/Submit) id q765flwf012685; Mon, 6 Aug 2012 05:41:47 GMT (envelope-from nobody) Message-Id: <201208060541.q765flwf012685@red.freebsd.org> Date: Mon, 6 Aug 2012 05:41:47 GMT From: William Ahern To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: standards/170403: wrong ntohs expression type tickling clang X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Aug 2012 05:50:08 -0000 >Number: 170403 >Category: standards >Synopsis: wrong ntohs expression type tickling clang >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-standards >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Mon Aug 06 05:50:01 UTC 2012 >Closed-Date: >Last-Modified: >Originator: William Ahern >Release: 9.0-RELEASE >Organization: >Environment: FreeBSD fbsd 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan 3 07:46:30 UTC 2012 root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64 >Description: I reported this bug to Apple a couple of years ago. Maybe it was inherited from FreeBSD. The source of the problem is ntohs/htons optimization. ntohs expands to (__builtin_constant_p(_x) ? __bswap16_const((__uint16_t)(_x)) : __bswap16_var(_x)) in /usr/include/machine/endian.h. The problem is that the promoted type of that ternary expression is int. But ntohs and htons are defined to have a return type of uint16_t. Normally, this is a case of no harm no foul because basically every value gets promoted to int eventually, when used as an operand or argument. Nonetheless, the clang printf analyzer complains when using the %hu format specifier. GCC keeps silent, which is probably why this issue never came to anybody's attention. $ make CC=clang foo clang -O2 -pipe foo.c -o foo foo.c:8:12: warning: conversion specifies type 'unsigned short' but the argument has type 'int' [-Wformat] printf("%hu\n", ntohs(i)); ~~^ ~~~~~~~~ %d 1 warning generated. >How-To-Repeat: // make CC=clang foo #include #include int main(void) { uint16_t i = 1234; printf("%hu\n", ntohs(i)); return 0; } >Fix: Cast the result of the __bswap16 expression to __uint16_t, either in the __ntohs/__htons macros, or in the __bswap16 macro directly like with __bswap16_const. >Release-Note: >Audit-Trail: >Unformatted: