Bug 12194 - htons() gives warning if compiled with gcc -Wconversion
Summary: htons() gives warning if compiled with gcc -Wconversion
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.10
: P2 normal
Target Milestone: ---
Assignee: Andreas Jaeger
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-05 07:03 UTC by Johan
Modified: 2014-06-30 06:30 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Johan 2010-11-05 07:03:34 UTC
The following (legitimate) code will generate a warning when both -O2 and 
-Wconversion is enabled but not when only -Wconversion is enabled. 

Code:
#include <arpa/inet.h>
// Nonsense code to illustrate problem
int main(void) {
    uint16_t portnbr=0;
    uint16_t n_portnbr = htons(portnbr);
    exit(0);
}

With optimization


$> gcc -std=gnu99 -Wconversion -Werror -O2 -c tsthtons.c
cc1: warnings being treated as errors
tsthtons.c: In function ‘main’:
tsthtons.c:6: error: conversion to ‘short unsigned int’ from ‘int’ may alter its value


and without


$>gcc -std=gnu99 -Wconversion -Werror  -c tsthtons.c
$>

in both cases

gcc --version
gcc (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839]
Copyright (C) 2009 Free Software Foundation, Inc.


and gnu clib v2.10.1
Comment 1 Ulrich Drepper 2010-11-10 07:54:40 UTC
Changed in git.
Comment 2 Anders Kaseorg 2012-06-20 05:56:27 UTC
Shouldn’t the same change (http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=69da074d7adfab7b57004a0dea9403a928e310a5) be applied to sysdeps/s390/bits/byteswap.h and the generic bits/byteswap.h?
Comment 3 Andreas Jaeger 2012-06-20 07:19:45 UTC
thanks, patch send: http://sourceware.org/ml/libc-alpha/2012-06/msg00550.html
Comment 4 Andreas Jaeger 2012-06-21 13:52:12 UTC
Fixed for glibc 2.16 now with:

commit 2174c6dd8555f654c30df2f8f3321b69e0f736f8
Author: Andreas Jaeger <aj@suse.de>
Date:   Thu Jun 21 15:49:33 2012 +0200

    Avoid -Wconversion warning for htons