From patchwork Wed Sep 30 15:26:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Blain via GitGitGadget X-Patchwork-Id: 11809449 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3C5F5618 for ; Wed, 30 Sep 2020 15:26:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1832F20759 for ; Wed, 30 Sep 2020 15:26:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="abu6wBr4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730892AbgI3P0h (ORCPT ); Wed, 30 Sep 2020 11:26:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730835AbgI3P0f (ORCPT ); Wed, 30 Sep 2020 11:26:35 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9351BC061755 for ; Wed, 30 Sep 2020 08:26:35 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id x23so2148452wmi.3 for ; Wed, 30 Sep 2020 08:26:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=fLx/oqN4eboVCzSUB4omppsZm0evzi7MY+4v/FyZl7s=; b=abu6wBr4vCrp09W5xsENwfpvv8MiK6jIE3W/pkj90L/g+i5kNFKo0X7uz7ufMaeH/b euaAbV0eYr9e36sqCQ062Kza3jpfD/5TGlrofjVhR8uZSQIvRp4adxEsiDV0cCXGMI5l XtiCCJWxCKX0cUnn0d61igv+XRaWzfChxsQdoio3tdi3b0CSA/KNlzpqj4+neXzdOy05 0FWWRUDjzc8rJxz3eMmtFukV5CC1T8sMmY+HVGTsidfVCnEbyKUrdGcdLrUJCq+dYqOI zvYIzT0jGj71MGy/RWJBTj1jQ6x6yUZI4LvstJ+OmXw5vriJSAZdHsIZ2triz+FRrsfU nFCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=fLx/oqN4eboVCzSUB4omppsZm0evzi7MY+4v/FyZl7s=; b=RQesZodPrFIGOTM1Q4xDI6kF2hl/m/AT5R2SJI1kSMtI+N2GY/tZuQUjmXj0EaCncL qr5YTXhQYQmYkh0cHWELI/sXSV5aDZNQs8xr0xAVmIPxu0G03yxvZuGSFXrr6C+wrUyp rS4OpKim1QPNNlV5tnjHMykK+OMOHpFi0UeJ4OtoiaFu3btp7u+bK0vHouXnwSw9WhBQ 8m/pY8aMizrhv7YqrDMclDJvpUWGfOmEbkQxxzlwqynJ9N0MUtoP7oN285MUg9MXlydl Z95St32XUOqkS7xWEMI8QDWSp9UX62rTmluPBymXfpGSgue7vDwwQVRj2sX5lJi0HVJk OrFg== X-Gm-Message-State: AOAM532vO189USJTl94PPUj0WyPk5xH6j9/puQRqRjL7YBG04HEfScKe mgsAUMCRiz1b/a7TQWW65IFER/b2/NI= X-Google-Smtp-Source: ABdhPJyaXBRMWRDqhxqocDB/yDgtvtpQTUZixx1iwYzYgQ5ULGg37SN2Qoj66HtjEw2ntZ8M2/cCPA== X-Received: by 2002:a1c:4e02:: with SMTP id g2mr3475751wmh.3.1601479594070; Wed, 30 Sep 2020 08:26:34 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id i9sm3316416wma.47.2020.09.30.08.26.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 08:26:33 -0700 (PDT) Message-Id: <7643a766b7dedb32bbe46689b01a9009e16277f9.1601479585.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Junio C Hamano via GitGitGadget" Date: Wed, 30 Sep 2020 15:26:24 +0000 Subject: [PATCH v4 10/10] hashmap_for_each_entry(): workaround MSVC's runtime check failure #3 Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Sibi Siddharthan , =?utf-8?b?xJBvw6Bu?= =?utf-8?b?IFRy4bqnbiBDw7RuZw==?= Danh , SZEDER =?utf-8?b?R8OhYm9y?= , Eric Sunshine , Johannes Schindelin , =?utf-8?q?=C3=98ystein?= Walle , Johannes Schindelin , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Junio C Hamano The OFFSETOF_VAR(var, member) macro is implemented in terms of offsetof(typeof(*var), member) with compilers that know typeof(), but its fallback implemenation compares &(var->member) and (var) and count the distance in bytes, i.e. ((uintptr_t)&(var)->member - (uintptr_t)(var)) MSVC's runtime check, when fed an uninitialized 'var', flags this as a use of an uninitialized variable (and that is legit---uninitialized contents of 'var' is subtracted) in a debug build. After auditing all 6 uses of OFFSETOF_VAR(), 1 of them does feed a potentially uninitialized 'var' to the macro in the beginning of the for() loop: #define hashmap_for_each_entry(map, iter, var, member) \ for (var = hashmap_iter_first_entry_offset(map, iter, \ OFFSETOF_VAR(var, member)); \ var; \ var = hashmap_iter_next_entry_offset(iter, \ OFFSETOF_VAR(var, member))) We can work around this by making sure that var has _some_ value when OFFSETOF_VAR() is called. Strictly speaking, it invites undefined behaviour to use NULL here if we end up with pointer comparison, but MSVC runtime seems to be happy with it, and most other systems have typeof() and don't even need pointer comparison fallback code. Signed-off-by: Junio C Hamano Signed-off-by: Johannes Schindelin --- hashmap.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hashmap.h b/hashmap.h index ef220de4c6..b011b394fe 100644 --- a/hashmap.h +++ b/hashmap.h @@ -449,7 +449,8 @@ static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map, * containing a @member which is a "struct hashmap_entry" */ #define hashmap_for_each_entry(map, iter, var, member) \ - for (var = hashmap_iter_first_entry_offset(map, iter, \ + for (var = NULL, /* for systems without typeof */ \ + var = hashmap_iter_first_entry_offset(map, iter, \ OFFSETOF_VAR(var, member)); \ var; \ var = hashmap_iter_next_entry_offset(iter, \