From patchwork Sat Aug 8 16:11:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11706377 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 18E7014DD for ; Sat, 8 Aug 2020 16:11:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 013FC20748 for ; Sat, 8 Aug 2020 16:11:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y18WYNle" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726248AbgHHQL5 (ORCPT ); Sat, 8 Aug 2020 12:11:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgHHQL5 (ORCPT ); Sat, 8 Aug 2020 12:11:57 -0400 Received: from mail-ej1-x644.google.com (mail-ej1-x644.google.com [IPv6:2a00:1450:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 406C6C061756 for ; Sat, 8 Aug 2020 09:11:57 -0700 (PDT) Received: by mail-ej1-x644.google.com with SMTP id f24so5168281ejx.6 for ; Sat, 08 Aug 2020 09:11:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6W3Zwgq830k+6n2foagKBbA9PQmZ9LsVQps0k5iUjBY=; b=Y18WYNlefQkUdjSQvns3nOTmfX1kGQTGSc4VfwQLQhetrxetXDmETaU8PECYrNy0WG z4vcQ4BEExiPPnaRY5iY9aOMu1BRj5eKAr9+ogqrmCwabC8pw04bd7V6GZY6ba3x5HLk Q4k66wdP0oeRuxNMRJEFgMlEF20BIP8fH6POLfszJ9cb5z7BQA4ob838hC8UZPbFHk2D RMAvNRT9zKWpbqLwCeBAnWJUv40wGEAT1uD3XhjtTgGV6jywFFuwliEZEhXJkGHUFj2g A8e3GTFT/SN7cZBtG74L1yszaNQlXfHumxrY3tdeJ/mdd/hj5yTnTNX27UWWlCOpUUQN cOzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6W3Zwgq830k+6n2foagKBbA9PQmZ9LsVQps0k5iUjBY=; b=H+0bYM6yRd5HgBQfb/CZt2tfVPdl/qc2fcB/TEUPtr1T9gIDNyCJSD8PbCmvzZcWYw 9tfqE2TEbpotkotbIRDmVsh1a11EupZlkGFIGsbwiMIVeqZ0LFKIyLCxaJdohkZjjq+l 1YyErpcVPMKf9NG/y8qwW62ZHLxCcpI6Nqu55iyWm68Q1wooRJ7dTN3Cl8EqOy8dHq6y 0xlCPqt3vfXiS3+L7s1hgipoF8gRF5YYXUo1FW/rb566TW5nMMZTUpWOhYHjewcMgsRK WHtTx3WjCFnPvxkf0k+sQ4WAg3mK7dgUdUa1nsDekPtaWFzIP0+P50uxtFQPEBnYUucD 6kAg== X-Gm-Message-State: AOAM530PyhC8YJhabBkMIPKAFIYsxXRrRaNhNgqb3SKA+5XacXjn7NK7 7fkL79KAwbG6K+OMRd7YLwNT/QE+ X-Google-Smtp-Source: ABdhPJyzWq0bGqqPUufCOiLYH94ros/XRdiRxOxwbuNcDVgUA2U2rtk29tKFdj1vyVoDbQQPaeBJjw== X-Received: by 2002:a17:907:94ca:: with SMTP id dn10mr14348358ejc.110.1596903115537; Sat, 08 Aug 2020 09:11:55 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:658a:a334:d0cb:45f5]) by smtp.gmail.com with ESMTPSA id g25sm8145725edp.22.2020.08.08.09.11.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 09:11:55 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 1/6] wstring: add support for evaluation of wide string Date: Sat, 8 Aug 2020 18:11:38 +0200 Message-Id: <20200808161143.28272-2-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> References: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Evaluation doesn't know about wide strings. Fix this by: 1) selecting the right base type (char_ctype vs wchar_ctype) 2) adapting the type, size & alignment of the underlying array to this base type. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/evaluate.c b/evaluate.c index dddea76182ad..acad11ab2712 100644 --- a/evaluate.c +++ b/evaluate.c @@ -102,9 +102,10 @@ static struct symbol *evaluate_string(struct expression *expr) struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL); struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING); unsigned int length = expr->string->length; + struct symbol *char_type = expr->wide ? wchar_ctype : &char_ctype; sym->array_size = alloc_const_expression(expr->pos, length); - sym->bit_size = bytes_to_bits(length); + sym->bit_size = length * char_type->bit_size; sym->ctype.alignment = 1; sym->string = 1; sym->ctype.modifiers = MOD_STATIC; @@ -117,10 +118,10 @@ static struct symbol *evaluate_string(struct expression *expr) initstr->string = expr->string; array->array_size = sym->array_size; - array->bit_size = bytes_to_bits(length); - array->ctype.alignment = 1; + array->bit_size = sym->bit_size; + array->ctype.alignment = char_type->ctype.alignment; array->ctype.modifiers = MOD_STATIC; - array->ctype.base_type = &char_ctype; + array->ctype.base_type = char_type; array->examined = 1; array->evaluated = 1; From patchwork Sat Aug 8 16:11:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11706379 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 41B82138A for ; Sat, 8 Aug 2020 16:11:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29F5920748 for ; Sat, 8 Aug 2020 16:11:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IF7vess1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726256AbgHHQL7 (ORCPT ); Sat, 8 Aug 2020 12:11:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgHHQL6 (ORCPT ); Sat, 8 Aug 2020 12:11:58 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D348C061756 for ; Sat, 8 Aug 2020 09:11:58 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id g19so5140382ejc.9 for ; Sat, 08 Aug 2020 09:11:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=p0XoFfodWfClj21A+5Z0dnnv+IeEGHHD8atYPrz0sRE=; b=IF7vess1X/IpIsZkGqlc4qK9qLyBCxy4s6e5TS72j/H9DMvgPMEgZ/2SBDWnVq6Gjt bL/CpupKv/nETNIY4jpA0FUWWN3GlS8GwXhCmie/QI1Dj3X4zs8uZC63WIazXvia5V1F fqFZbe4GC8xS0FCb6J3E3TdOASJFbBcjBOkebyx5J8GOUWA+OFphitIjg4eeDrEo4wPS IznqH3zJEFLJYiZf2zLv9hBJnPEPoEETDa5OZ0rEVoM6C+qBVXiVNqs4vTxtau+2qWEw MIyFqvyBZ1I9APNDYXF1RJKjqyTPjxg7a5vhkztwGED8TiDD9hE1EFxuOcoxpv+nSjY9 K4DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=p0XoFfodWfClj21A+5Z0dnnv+IeEGHHD8atYPrz0sRE=; b=DMHy0+Q04WtNFj4lKnw54iEU9jIxxzlhbt2uXHdeKho56ukL/G1Ef3t27/pMlEeefp qA9FOJMcaH9J8jlOv5w0vWV+MJ+oSCtHMYk465hvxsKfEWDO+ifoALA6EHpAMylBzxgc onMNCgG61wauMNz1oDiTAtg8JhAgwdyEwrmOw327iZa7Q+jPC0nh1APW0K5TWYZzgcv2 KAisvBem1u6UkBBvpk/utFVRKRbRrkjBBss9PIu+/JIXZf+sOCsTdkH0GY+DQ9DSU9/l dArJLa68oA3TN4DjPDCIOozXS5M18ITMgJrde47a7OatFL3pLqwspYIofNOmY6I6hfIX dDdw== X-Gm-Message-State: AOAM531jmJLITo4HcduosyTB1cM3MxKAPauLwk5xUvCH/sIjEzqz8mBf CZR7u7riAmzLPd+8Ui42YN3PzltE X-Google-Smtp-Source: ABdhPJxCldOteiZ07uCf6KLEFCGqe8Sl1vKV4l1FDbEWlazSIcNYS6Vv4xJekEjqCXpYysBOO/9/oQ== X-Received: by 2002:a17:906:27d7:: with SMTP id k23mr14345711ejc.74.1596903116668; Sat, 08 Aug 2020 09:11:56 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:658a:a334:d0cb:45f5]) by smtp.gmail.com with ESMTPSA id g25sm8145725edp.22.2020.08.08.09.11.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 09:11:56 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 2/6] wstring: add support for checking size in string initializer Date: Sat, 8 Aug 2020 18:11:39 +0200 Message-Id: <20200808161143.28272-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> References: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org A warning is given for string initializers if the LHS array is not large enough to contains the string. But this check doesn't knowns about wide strings. Fix this by selecting the correct char type and use this type for the size calculations. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 6 ++++-- validation/init-wstring.c | 41 +++++++++++++++++++++++++++++++++++++++ validation/init_cstring.c | 2 ++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/evaluate.c b/evaluate.c index acad11ab2712..bbfa77c3a79d 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2841,10 +2841,12 @@ String: *p = *e; type = evaluate_expression(p); if (ctype->bit_size != -1) { - if (ctype->bit_size + bits_in_char < type->bit_size) + struct symbol *char_type = e->wide ? wchar_ctype : &char_ctype; + unsigned int size_with_null = ctype->bit_size + char_type->bit_size; + if (size_with_null < type->bit_size) warning(e->pos, "too long initializer-string for array of char"); - else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) { + else if (Winit_cstring && size_with_null == type->bit_size) { warning(e->pos, "too long initializer-string for array of char(no space for nul char)"); } diff --git a/validation/init-wstring.c b/validation/init-wstring.c new file mode 100644 index 000000000000..846b6e2c3efd --- /dev/null +++ b/validation/init-wstring.c @@ -0,0 +1,41 @@ +static const __WCHAR_TYPE__ ok0[] = L"abc"; +_Static_assert(sizeof(ok0) == 4 * sizeof(__WCHAR_TYPE__)); +static const __WCHAR_TYPE__ ok1[] = (L"abc"); +_Static_assert(sizeof(ok1) == 4 * sizeof(__WCHAR_TYPE__)); +static const __WCHAR_TYPE__ ok2[] = { L"abc" }; +_Static_assert(sizeof(ok2) == 4 * sizeof(__WCHAR_TYPE__)); + +static const __WCHAR_TYPE__ ok3[4] = L"abc"; +_Static_assert(sizeof(ok3) == 4 * sizeof(__WCHAR_TYPE__)); +static const __WCHAR_TYPE__ ok4[4] = (L"abc"); +_Static_assert(sizeof(ok4) == 4 * sizeof(__WCHAR_TYPE__)); +static const __WCHAR_TYPE__ ok5[4] = { (L"abc") }; +_Static_assert(sizeof(ok5) == 4 * sizeof(__WCHAR_TYPE__)); + +static const __WCHAR_TYPE__ ok6[7] = L"abc"; +_Static_assert(sizeof(ok6) == 7 * sizeof(__WCHAR_TYPE__)); +static const __WCHAR_TYPE__ ok7[7] = (L"abc"); +_Static_assert(sizeof(ok7) == 7 * sizeof(__WCHAR_TYPE__)); +static const __WCHAR_TYPE__ ok8[7] = { (L"abc") }; +_Static_assert(sizeof(ok8) == 7 * sizeof(__WCHAR_TYPE__)); + +static const __WCHAR_TYPE__ *ptr[] = { L"abc" }; +_Static_assert(sizeof(ptr) == sizeof(void *)); + +static struct s { + const __WCHAR_TYPE__ str[4]; +} str = { L"xyz" }; + +static const __WCHAR_TYPE__ ko3[3] = L"abc"; +static const __WCHAR_TYPE__ ko2[2] = L"abc"; + +/* + * check-name: init-wstring + * check-command: sparse -Winit-cstring $file + * check-known-to-fail + * + * check-error-start +init-wstring.c:29:38: warning: too long initializer-string for array of char(no space for nul char) +init-wstring.c:30:38: warning: too long initializer-string for array of char + * check-error-end + */ diff --git a/validation/init_cstring.c b/validation/init_cstring.c index 00eca20aa506..bac814e42907 100644 --- a/validation/init_cstring.c +++ b/validation/init_cstring.c @@ -1,11 +1,13 @@ static struct alpha { char a[2]; } x = { .a = "ab" }; +static const char str[2] = "abc"; /* * check-name: -Winit-cstring option * * check-command: sparse -Winit-cstring $file * check-error-start init_cstring.c:3:14: warning: too long initializer-string for array of char(no space for nul char) +init_cstring.c:4:28: warning: too long initializer-string for array of char * check-error-end */ From patchwork Sat Aug 8 16:11:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11706381 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 B0FF5138A for ; Sat, 8 Aug 2020 16:12:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9979020748 for ; Sat, 8 Aug 2020 16:12:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pUMfylmD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726296AbgHHQMA (ORCPT ); Sat, 8 Aug 2020 12:12:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgHHQMA (ORCPT ); Sat, 8 Aug 2020 12:12:00 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AADBC061756 for ; Sat, 8 Aug 2020 09:11:59 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id bo3so5146156ejb.11 for ; Sat, 08 Aug 2020 09:11:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Icp4hhn0az23eIZYVDnmbGyHTZnxC2AyA0XHZdihlQc=; b=pUMfylmDLtIzZd6/L5DwgCmbsX2jE7bg8gsNnmOEn2xQHCMeFuFjHcyLmr7m02qQmk UrU9LkWns3D7dMkpKGnlaEsbarxUbxSYrD2tzIt72Kpa+v7fPgbBea/Oe5HtqZFd5tfS Lx+TOpYw1m2s6iXOuP54oRz82HFFDcdpSxa1OxPYZkJzq7C+HQxNxCFv0a3H57NuWafr NaIurrrR9hFJWnTfoE4UsLxpZBbw6efHzhO7MInEcBFiSWUd77O1dl6T2XPwbOLbEo/m sVttbxMEl3aBN+WJpe9ONu4cWFdkvUFURZR/h3t079bmkZ+EVSdkqxp1MRxoaCvjadfr Murg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Icp4hhn0az23eIZYVDnmbGyHTZnxC2AyA0XHZdihlQc=; b=dsnkkN1Rvdx6FKcGPVwSHqcnbN3cQyFyMV7lOZYRzdWui5gi3Vt/ETw1axEVD7jMqq kTd4h1G/LiEilJumGYfw7g41sC9G5xSeP8QwvFf2ONnUDDk/TVtq84nGTe1bDj2bLs8M fQF/5IRBMDt9VB3Ko1twP0wKZE68o9TYUbLdZxfovZhQlS8t9oCDv+vg8Tm3INYrnC2D kR5EOL8kCXIHT/rvVbvoqhLMXVGlI9diwARHmPvip2TpuEt7dngV5wsmXb867N8CrcRX 8v6FtyJdFI4y1bmbZc6rf8Yq7Be9lsD9lc9b42P+1dSo0SPJDJa4Y9LGaESJ8F+FIUUT Y9SA== X-Gm-Message-State: AOAM530uA7dVSSd2oPuPG6VuC5ZEmyQsRREV53XsFcE+F1IHgibHP8Sv xiDhcaRKHfVFsbo/MeFgj5u9XGz2 X-Google-Smtp-Source: ABdhPJy1zMA4Qp2l36qxf2gQ3PfI9gLlNW5Vah/WpV3vZ3DQay+OWB9/0YsgQ7n2xfYYTzs1atdl5w== X-Received: by 2002:a17:906:260f:: with SMTP id h15mr14304554ejc.48.1596903117896; Sat, 08 Aug 2020 09:11:57 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:658a:a334:d0cb:45f5]) by smtp.gmail.com with ESMTPSA id g25sm8145725edp.22.2020.08.08.09.11.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 09:11:57 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 3/6] wstring: add support for examination of string initialization Date: Sat, 8 Aug 2020 18:11:40 +0200 Message-Id: <20200808161143.28272-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> References: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org The examination of a string initializer doesn't know about wide strings. The only thing needed is if the base type is some kind of char but for wide chars, this type is the same as 'int' and an array of ints can't be treated the same as an array of chars. So, do the detection for wide string initializers as: 1) check that the LHS base type is wchar_ctype 2) check that the RHS is a kind of string expression (possibly between braces or parenthesis, recursively). Signed-off-by: Luc Van Oostenryck --- symbol.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/symbol.c b/symbol.c index 6fcb1b150d27..7f0c85580f06 100644 --- a/symbol.c +++ b/symbol.c @@ -307,6 +307,29 @@ void merge_type(struct symbol *sym, struct symbol *base_type) merge_type(sym, sym->ctype.base_type); } +static bool is_wstring_expr(struct expression *expr) +{ + while (expr) { + switch (expr->type) { + case EXPR_STRING: + return 1; + case EXPR_INITIALIZER: + if (expression_list_size(expr->expr_list) != 1) + return 0; + expr = first_expression(expr->expr_list); + break; + case EXPR_PREOP: + if (expr->op == '(') { + expr = expr->unop; + break; + } + default: + return 0; + } + } + return 0; +} + static int count_array_initializer(struct symbol *t, struct expression *expr) { int nr = 0; @@ -321,6 +344,8 @@ static int count_array_initializer(struct symbol *t, struct expression *expr) */ if (t->ctype.base_type == &int_type && t->rank == -2) is_char = 1; + else if (t == wchar_ctype && is_wstring_expr(expr)) + is_char = 1; switch (expr->type) { case EXPR_INITIALIZER: { From patchwork Sat Aug 8 16:11:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11706383 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 77A4A722 for ; Sat, 8 Aug 2020 16:12:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 55FF920716 for ; Sat, 8 Aug 2020 16:12:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nybDJ8XT" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726316AbgHHQMB (ORCPT ); Sat, 8 Aug 2020 12:12:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726291AbgHHQMA (ORCPT ); Sat, 8 Aug 2020 12:12:00 -0400 Received: from mail-ed1-x543.google.com (mail-ed1-x543.google.com [IPv6:2a00:1450:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5560DC061A27 for ; Sat, 8 Aug 2020 09:12:00 -0700 (PDT) Received: by mail-ed1-x543.google.com with SMTP id c10so3403606edk.6 for ; Sat, 08 Aug 2020 09:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=i0YBbxB9z/tlOZGarQXmwSVJk5l+FnIa/DLNKdhToNQ=; b=nybDJ8XTY3lmqTerTTS+lRdDhAaJzYKm1DTkzsQSusrRTP3ZJZvhFDnilgDrtFs72O aLS2oQ1CvqpLiQI68NMwNeqt8Ajm/llBaqz/V+okCVg4SnFepdWFptcpqmgTlB4DGi7m BCTuVyJCRmsOmeke0b7h2qPZjdSpVSC0NghqeY0QXPSj7+AoKS10QSOptoU7wiTu0Wmc 6vpgSQ6CzL8Y9vrfA1y2UBp7JWePc0kJs8EAUHNx8C9vZMPfjhiDQ9ZUyZNVbBRInDHa kxkbkh8N6gQK46kIjSXhAeDW5606QYI7f/4aCfj6x0yEVUmiQOxHsBA492A3D4B6XYCe fzvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=i0YBbxB9z/tlOZGarQXmwSVJk5l+FnIa/DLNKdhToNQ=; b=CQfrMw+ikroPCptpwK028lXvF7QWbUrFpcsYe+/sGoIi1u1ZRIO1mn3MaucMU8ySly Jxm7UZ/Kb9ndO94x2IrT65BkTGpjF09kilOdOi1BfcLAQnCWN6zHG6eNQ9N86vL78YgH onJz6iuPpnbhpg7FGZ+OQbaNemY9xCRkK7O6Lq9j/d4qD0vDckC4BF+6y7i2mZ17PZCR dn0EvFIhqt6QhKtamDClI7F33HimE6nHwU9Z+pcL5D1xvlaDhv/HXFQDpC+7qIhSsdHE k5rwHNVywXhNkwtR14MTCjoRfPbSsy7MxUCsYLy0VF85Waqu/thqboxw48EgoKVwzbdp 9HlA== X-Gm-Message-State: AOAM53079/8IuLLk9icuqA5kAGmPsTZQsym39F/N0uGcriwqdiYzQ9cB eQ3FD1Jrh/cLcN9fP6C2FTgWio/Y X-Google-Smtp-Source: ABdhPJxsZnQKN4p5rOo/kCBbPzOrJhu/pG1K+N4NLbpbHoUZtjLILtpLltuwh+5UxoLflPQJ2bNBtw== X-Received: by 2002:a05:6402:1457:: with SMTP id d23mr13998316edx.149.1596903118840; Sat, 08 Aug 2020 09:11:58 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:658a:a334:d0cb:45f5]) by smtp.gmail.com with ESMTPSA id g25sm8145725edp.22.2020.08.08.09.11.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 09:11:58 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 4/6] wstring: add helper is_wchar_type() Date: Sat, 8 Aug 2020 18:11:41 +0200 Message-Id: <20200808161143.28272-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> References: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Like is_byte_type() but for wide chars. Signed-off-by: Luc Van Oostenryck --- symbol.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/symbol.h b/symbol.h index 8e7a286019c7..0e15f7bf6ba8 100644 --- a/symbol.h +++ b/symbol.h @@ -430,6 +430,13 @@ static inline int is_byte_type(struct symbol *type) return type->bit_size == bits_in_char && type->type != SYM_BITFIELD; } +static inline int is_wchar_type(struct symbol *type) +{ + if (type->type == SYM_NODE) + type = type->ctype.base_type; + return type == wchar_ctype; +} + static inline int is_void_type(struct symbol *type) { if (type->type == SYM_NODE) From patchwork Sat Aug 8 16:11:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11706385 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 9E5A814DD for ; Sat, 8 Aug 2020 16:12:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8688220748 for ; Sat, 8 Aug 2020 16:12:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oG3gUe6l" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726291AbgHHQMC (ORCPT ); Sat, 8 Aug 2020 12:12:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgHHQMB (ORCPT ); Sat, 8 Aug 2020 12:12:01 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 527B2C061756 for ; Sat, 8 Aug 2020 09:12:01 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id c16so5142383ejx.12 for ; Sat, 08 Aug 2020 09:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rEbn7nCT3xSMtCxm5v/MUr79gulE23OQ0NZ7sa1vfi4=; b=oG3gUe6lf54D5Cbk19GjA4nYua/zJ8Nz1U7ExRr6j+EnpnMtfvUljg4QuBVXyelS1n p+rxRHsYyqJqz6nAs+Dc49M0/twUmlqT/iJmtFh+3Kv8mtM+60f3C8JfkC4Er1DkuX+w eaS0LVvy6Q5qm0qpK2qybRsg4tmLaNmplzlq7vws5KCMkV3euRQ+dyZYGkeKOhUkmKNZ AvBCmtMERDTccHAhbphnqRCmxsQzAVp76c9SvXJAw6jXE2Cuo8ySfv8K/dzmuwJhkjvs fuo2nLZ2hr51+PWMh+VutJLtvBSo4WSY7uADPGqfHpewh0qdcp9WwfUnBwEFex1vGwNI 2bFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rEbn7nCT3xSMtCxm5v/MUr79gulE23OQ0NZ7sa1vfi4=; b=UBgWpnggFqF1KkQs/r6X3N7IlOu/mL7rIohNgF4ym/W7b3qd8m2cif8zXwAlwVtu4J fvNVC3buR8TP7VHAtK9bAakOphpy9d/SM3UkyuPOPvCyXQufeao42PkKsKEtJCayemKQ l2XDdItWLMcbCkiK8MRpD3uDn4qp6E72zCcF0Mc8AxdyRJDt9JaLWMc8Bb6mscGmRH/b i1N9rmAAeMxZhvlnRf0olI7EmdB0KiY09zM3m5FPt9y3xWnCHDX86gnpM0HvcUzftVks pyiitJ0Z4UyPhZkJupnwYw3i9cCQbuuVgnNeLTF2iwGE6vEAlfrOfR2mrzgRIxvpAlQv UP2g== X-Gm-Message-State: AOAM532vtM4XJBMcu3x+ShB7/Y3S59uHgcLbnGFdIYqPVp2MdqARJDIA +cPzZrVqRMpBJb3rJy3GAVsXI53e X-Google-Smtp-Source: ABdhPJwsdevOIgyeDJav55a8o0alSsTr9OINWJuI5XQjQ1GTor5cvoXZ3Y77B1R1MIXAISOW8EAVow== X-Received: by 2002:a17:906:e0ce:: with SMTP id gl14mr15250273ejb.393.1596903119806; Sat, 08 Aug 2020 09:11:59 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:658a:a334:d0cb:45f5]) by smtp.gmail.com with ESMTPSA id g25sm8145725edp.22.2020.08.08.09.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 09:11:59 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 5/6] wstring: extend is_string_type() to also detect wide strings Date: Sat, 8 Aug 2020 18:11:42 +0200 Message-Id: <20200808161143.28272-6-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> References: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org When evaluating initializers, it must be known if it is for a string or not. But sparse doesn't known about wide strings. Fix this by modifying is_string_type() to use is_wchar_type() in addition of is_byte_type(). Signed-off-by: Luc Van Oostenryck --- evaluate.c | 5 ++++- validation/init-wstring.c | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/evaluate.c b/evaluate.c index bbfa77c3a79d..8d09c560e7fe 100644 --- a/evaluate.c +++ b/evaluate.c @@ -406,7 +406,10 @@ static inline int is_string_type(struct symbol *type) { if (type->type == SYM_NODE) type = type->ctype.base_type; - return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type); + if (type->type != SYM_ARRAY) + return 0; + type = type->ctype.base_type; + return is_byte_type(type) || is_wchar_type(type); } static struct symbol *bad_expr_type(struct expression *expr) diff --git a/validation/init-wstring.c b/validation/init-wstring.c index 846b6e2c3efd..d9ce3b3c883b 100644 --- a/validation/init-wstring.c +++ b/validation/init-wstring.c @@ -32,7 +32,6 @@ static const __WCHAR_TYPE__ ko2[2] = L"abc"; /* * check-name: init-wstring * check-command: sparse -Winit-cstring $file - * check-known-to-fail * * check-error-start init-wstring.c:29:38: warning: too long initializer-string for array of char(no space for nul char) From patchwork Sat Aug 8 16:11:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11706387 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 5B84E14DD for ; Sat, 8 Aug 2020 16:12:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 44AA520748 for ; Sat, 8 Aug 2020 16:12:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CS/wQlqD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726262AbgHHQMD (ORCPT ); Sat, 8 Aug 2020 12:12:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgHHQMC (ORCPT ); Sat, 8 Aug 2020 12:12:02 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B7CCC061756 for ; Sat, 8 Aug 2020 09:12:02 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id g19so5140482ejc.9 for ; Sat, 08 Aug 2020 09:12:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0gGvYsYI7pAIHGRHTOtfrbaAjnfrDtx4Jcwk5RW42xw=; b=CS/wQlqDwKom+AMhzrJN3Aw3Iy15pY6vtUrypWeSM+my1pSdJRqjvyZNvUFOQRmHBT VGQq0d0yBjazwiDaZM1hGpaKayg/vJrWeddBGZclB7U6FbfeMDWOjm1rUp2mVjd8srY0 fqAocssn+8o+W/w6hI30VTme54xg7g28+WnGO/D226P5PB60cERVN4pFUSBVNdlMMryQ dJYTEIvjBGsxQm3JCsg1ArYn6q93aBl1K+h30FgcsEC+6jS0j/8CBTGqMwFbGf1lMfRh nrInG2dQm9fOIAM9sA2Jy131NHCzu7kLrPDvJ2FFyfv6KIUzPSikhQZt4rnQq5Gve+TM JEVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0gGvYsYI7pAIHGRHTOtfrbaAjnfrDtx4Jcwk5RW42xw=; b=kizas+ZpK/zm0LBNlLordfWCKgOrcvppJHNaYsi+1WCIhx1VAD3eFgpMVZhB5e8bxC kMDI1zUzT5bDBOdeIhiSvD1l7iDtP7OU4J7Rtvou7gbpnNs6r8MNRW8cS+wSWapneg+W uTht5JohxWQo4DypWEmW1oqJoJ7j+R99WDr6z6KjJYdbuzUSrAVucSCd/rouKzvlh3QA +dTmTTOOLQH1LdfPnMu2mBInw0QperY5x20LNFfwQnzM/DZFjrCv+M+LwNHPnsE81ehE QhFCFnEmAwXCn73UWp6jborIfOs6dIGk14RKc9ohJtl8RQgKPhI72myPcrqN7eGHkti5 Ge/Q== X-Gm-Message-State: AOAM530U/WUgTeUcm+kLlPGlgY0cLxLOM7Vwo0tsF1GfwWUgIjjf8VIS as2fMbA6M4APJyWDlL4a7IWLWDyw X-Google-Smtp-Source: ABdhPJybCAxMq7oimlOwMnDuIjzmzMxzWGXVY8SlsJbLzkDYDxrNWnZLLQsTl9ae11b28tsh/srDqg== X-Received: by 2002:a17:906:660b:: with SMTP id b11mr15066500ejp.509.1596903120783; Sat, 08 Aug 2020 09:12:00 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:658a:a334:d0cb:45f5]) by smtp.gmail.com with ESMTPSA id g25sm8145725edp.22.2020.08.08.09.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Aug 2020 09:12:00 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 6/6] wstring: call is_string_type() only when needed Date: Sat, 8 Aug 2020 18:11:43 +0200 Message-Id: <20200808161143.28272-7-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> References: <20200808161143.28272-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Just a tiny code reorganization to call is_string_type() only where & when needed. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/evaluate.c b/evaluate.c index 8d09c560e7fe..d2cf1c0ae3f8 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2764,7 +2764,6 @@ static struct expression *handle_scalar(struct expression *e, int nested) static int handle_initializer(struct expression **ep, int nested, int class, struct symbol *ctype, unsigned long mods) { - int is_string = is_string_type(ctype); struct expression *e = *ep, *p; struct symbol *type; @@ -2798,7 +2797,7 @@ static int handle_initializer(struct expression **ep, int nested, * pathologies, so we don't need anything fancy here. */ if (e->type == EXPR_INITIALIZER) { - if (is_string) { + if (is_string_type(ctype)) { struct expression *v = NULL; int count = 0; @@ -2819,7 +2818,7 @@ static int handle_initializer(struct expression **ep, int nested, /* string */ if (is_string_literal(&e)) { /* either we are doing array of char, or we'll have to dig in */ - if (is_string) { + if (is_string_type(ctype)) { *ep = e; goto String; }