Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117819 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97891 invoked from network); 28 May 2022 18:02:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 May 2022 18:02:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D4970180084 for ; Sat, 28 May 2022 12:45:37 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 28 May 2022 12:45:37 -0700 (PDT) Received: by mail-yb1-f171.google.com with SMTP id h75so7129509ybg.4 for ; Sat, 28 May 2022 12:45:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=mpk9S1Fbk1hnZ5pLw56m2DRJlcBhLtp5LjsuLWZkQ8s=; b=cnLmWfkQwFQzVX8EZMtf8AuOxEijwazYFqxnIr5QNPTYlAHEBUsKKdoTt9stUqpLmc POcOJZPTAXcgdVuYOSQEzYKpXyA5Zwll8JrXVyRilJ6duekiswrJ2RdWcZm2IBymbfnB 7kM/SJpL9nTffIGBnG/IblXUuct3I7B7tIgYsblF1pvdgCvT87voUxNupjj3CMWQtmlK GCK2yVKUy3c1g7r78P90Ey828XNqPvNTHvcsXPLf27a3o06Mbc3p8BZ9tw4osQFocSdt j/i4/gdzL7hWppLqunJBXgu15/46ZqTLmy/FAgbAXDqJieJIPmWHJyhACRBhG8Bwr6Vt tlxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=mpk9S1Fbk1hnZ5pLw56m2DRJlcBhLtp5LjsuLWZkQ8s=; b=GQfT4dm7KFaZnlt82BsD5WvqvbSGEseOq+Z7LcTD9dKAX1UiKfcndnWi1qrTVWoo4R fEeKXZUtfwLIbZmuBCrMj9EdV4dt8bPfm9ghYkuR9NpD1uzoNlGIZVYji4XRYARhzl2o BcGGWYHnJyDr61mkfsbMMmRFqjoVWuHklUhX5UQ2Nln2WFLPKr0eF5r0/2x937zg/YCG 7IKJW4s4XjDGnEjUrqB0i8hUlacYICCI+RJgjPO1EmRk3Xu5JoQRLw0Ix0zgQfzJfJcM 0c24/kR2l8lmikzH/kUb+od7b44jeFL/4zNNfFKY7pB46RH+zBEeyxlycqPqcJuL6Rnu zUmg== X-Gm-Message-State: AOAM533ZWqYl7Fj9Y+girDTQ9Kq3Pb0U4Yel0qyinAEsFh7dI8IKvKbn /UZVwYgegHXsoykzIO7uWMh9bK2gR5mOjC0NiG09EWmW X-Google-Smtp-Source: ABdhPJx/WFdHr8wOG6WO3S+JB0vXePzk1FCxoQfSacdrZIDTphm6ZC1UvOFPQYGaMlKVvWDn7LwsO6MKULdyghxqnrg= X-Received: by 2002:a25:fe0d:0:b0:64d:915f:e73e with SMTP id k13-20020a25fe0d000000b0064d915fe73emr45740516ybe.447.1653767136307; Sat, 28 May 2022 12:45:36 -0700 (PDT) MIME-Version: 1.0 References: <1755E8B5-229B-47B2-BBAF-B5E014F5473D@craigfrancis.co.uk> <1180af01-080f-ee0a-3159-74bf7e0a8aea@gmail.com> <73F563E2-7C31-4B5E-A6B9-AE1BD05ADD1C@craigfrancis.co.uk> <24d35dc8-bd1d-3d90-99ac-ebdcc3b2a9e4@gmail.com> In-Reply-To: Date: Sat, 28 May 2022 14:42:29 -0500 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000b36f6b05e017aacb" Subject: Re: [PHP-DEV] NULL Coercion Consistency From: michael.babker@gmail.com (Michael Babker) --000000000000b36f6b05e017aacb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, May 27, 2022 at 9:36 PM Craig Francis wrote: > I know I keep going on about this very simply example, but it represents = a > fairly typical style of programming PHP, and I just do not understand wha= t > the problem with it is: > > ``` > $search =3D $request->input('q'); // Laravel, returns NULL when 'q' is no= t > defined. > > echo 'Results for ' . htmlspecialchars($search); > ``` If you want to go with this specific example, exactly as written, here is the exact issue I have with it. As already mentioned, `htmlspecialchars()` is documented and the implementing code requires a string argument. Passing a null value to it requires explicitly writing non-typesafe code, and requires understanding of how the PHP language handles type coercion, and how those rules are different based on whether the file calling that function has the `strict_types` declaration. So on a code review, I have to understand the context of the value passed through and know what context the language is operating in to determine if non-string values are allowed to avoid errors, and understand the semantics of PHP=E2=80=99s type coercion system to make = sure the output is something expected based on the input. Compare that to knowing the function explicitly only accepts a string value and you know you are working with a string through appropriate validation; for me, even in a loosely typed environment, I=E2=80=99d rather follow the documented paramet= er types instead of having to worry about all the other magic involved to make null work. Though, that input variable would never reach output in any of my projects to begin with if it were null or an empty string; both would land on a code path treated as no search being performed. So that example for me is also way oversimplified because it doesn=E2=80=99t match a real world workflow I= MO. On Fri, May 27, 2022 at 9:36 PM Craig Francis wrote: > Sorry, but I'm not following... if there is a benefit/reason for PHP to > reject NULL for `htmlspecialchars()`, and I'm just too stupid to see what > it is, I would have assumed that benefit/reason would also apply to the > HTML encoding function `e()` in Laravel. No, that would not automatically apply to Laravel=E2=80=99s helper function= , or any escaping functions in Twig, or escaping functions used in platforms without templating frameworks like WordPress. If it=E2=80=99s not OK for functions= that can call `htmlspecialchars()` to gracefully handle null to make sure those trigger the same type error as the core language, then along the same lines, I would argue that patches which add null coalescing or explicit typecasting to that parameter in libraries or applications should also be rejected because then they=E2=80=99re masking an error the language purpose= fully elected to emit. > --=20 - Michael Please pardon any errors, this message was sent from my iPhone. --000000000000b36f6b05e017aacb--