From owner-freebsd-hackers@freebsd.org Wed Jul 8 14:25:46 2020 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5FF11363EBB for ; Wed, 8 Jul 2020 14:25:46 +0000 (UTC) (envelope-from pjfloyd@wanadoo.fr) Received: from smtp.smtpout.orange.fr (smtp03.smtpout.orange.fr [80.12.242.125]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (Client CN "Bizanga Labs SMTP Client Certificate", Issuer "Bizanga Labs CA" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B21n52HYzz47RV for ; Wed, 8 Jul 2020 14:25:44 +0000 (UTC) (envelope-from pjfloyd@wanadoo.fr) Received: from wwinf1k08 ([10.223.65.51]) by mwinf5d06 with ME id 0eRi2300J16MGNa03eRiMd; Wed, 08 Jul 2020 16:25:42 +0200 X-ME-Helo: wwinf1k08 X-ME-Auth: cGpmbG95ZEB3YW5hZG9vLmZy X-ME-Date: Wed, 08 Jul 2020 16:25:42 +0200 X-ME-IP: 2.7.112.8 Date: Wed, 8 Jul 2020 16:25:42 +0200 (CEST) From: Paul FLOYD Reply-To: Paul FLOYD To: freebsd-hackers@freebsd.org Message-ID: <267885064.4984.1594218342653.JavaMail.www@wwinf1k08> In-Reply-To: References: Subject: re: strange output in c program MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [2.7.112.8] X-WUM-FROM: |~| X-WUM-TO: |~| X-WUM-REPLYTO: |~| X-Rspamd-Queue-Id: 4B21n52HYzz47RV X-Spamd-Bar: +++++ Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of pjfloyd@wanadoo.fr has no SPF policy when checking 80.12.242.125) smtp.mailfrom=pjfloyd@wanadoo.fr X-Spamd-Result: default: False [5.12 / 15.00]; HAS_REPLYTO(0.00)[pjfloyd@wanadoo.fr]; HAS_XOIP(0.00)[]; FREEMAIL_FROM(0.00)[wanadoo.fr]; TO_DN_NONE(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(0.00)[]; R_DKIM_NA(0.00)[]; ASN(0.00)[asn:3215, ipnet:80.12.240.0/20, country:FR]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[wanadoo.fr]; ARC_NA(0.00)[]; REPLYTO_EQ_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_SPAM_SHORT(1.69)[1.693]; MIME_GOOD(-0.10)[text/plain]; FREEMAIL_REPLYTO(0.00)[wanadoo.fr]; AUTH_NA(1.00)[]; NEURAL_SPAM_MEDIUM(1.05)[1.047]; RCPT_COUNT_ONE(0.00)[1]; DMARC_NA(0.00)[wanadoo.fr]; NEURAL_SPAM_LONG(0.98)[0.983]; RCVD_IN_DNSWL_NONE(0.00)[80.12.242.125:from]; R_SPF_NA(0.00)[no SPF record]; RWL_MAILSPIKE_POSSIBLE(0.00)[80.12.242.125:from]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jul 2020 14:25:46 -0000 As already said, when you have UB anything can happen. First recommendation. Listen to your compiler! paulf> clang -Weverything -g -o test2 test2.c test2.c:5:7: warning: variable 'b' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized] if(a >= 400) ^~~~~~~~ test2.c:11:22: note: uninitialized use occurs here printf("%d %d\n", b, c); ^ test2.c:5:4: note: remove the 'if' if its condition is always true if(a >= 400) ^~~~~~~~~~~~ test2.c:4:18: note: initialize the variable 'b' to silence this warning int a = 300, b, c; ^ = 0 clang is even telling you how to fix your errors. Even if the compiler can't pick up the error, you can also test at runtime: ==55524== Memcheck, a memory error detector ==55524== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==55524== Using Valgrind-3.17.0.GIT and LibVEX; rerun with -h for copyright info ==55524== Command: ./test2 ==55524== ==55524== Conditional jump or move depends on uninitialised value(s) ==55524== at 0x49B2E65: ??? (in /lib/libc.so.7) ==55524== by 0x49AF184: vfprintf_l (in /lib/libc.so.7) ==55524== by 0x49FB203: printf (in /lib/libc.so.7) ==55524== by 0x20133B: main (test2.c:11) A+ Paul