Delivered-To: mlmmj-tyndur-devel
Received: from sv1.famkaufmann.info [176.9.73.67]
	by a9b8c5461646 with IMAP (fetchmail-6.3.26)
	for <mlmmj@localhost> (single-drop); Fri, 12 Mar 2021 23:25:22 +0000 (GMT)
Received: from localhost ([172.29.0.7])
	by sv1.famkaufmann.info with LMTP
	id uLr8IUb4S2A9HQAALHnFKQ
	(envelope-from <kevin@tyndur.org>)
	for <mlmmj-tyndur-devel>; Fri, 12 Mar 2021 23:24:54 +0000
Delivered-To: tyndur-devel@tyndur.org
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tyndur.org; s=key2;
	t=1615591494; h=from:from:reply-to:reply-to:subject:subject:date:date:
	 message-id:message-id:to:to:cc:mime-version:mime-version:
	 content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=3RDFqNQ5wD8q1HV49yrmUp8NH+VPjH8qj12lePUqxag=;
	b=kbCdIpO2CYvgPjQFL226jEOFIgwv65+VKBC7T1c1b/BHXgY/Kioa/nqStYBdTUfQxsJUcT
	E46as6NtLqP+wuinBu+KAIo3wemuhEXthMTnR7/F111LxInPmlf2LMZTtshDmA5UtubmZJ
	1ux9NJ+Y0VnQtsZCw4QsVqUQIOnohSc=
Received: 
	by sv1.famkaufmann.info (OpenSMTPD) with ESMTPSA id 7dc0aef9 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO)
	for <tyndur-devel@tyndur.org>;
	Fri, 12 Mar 2021 23:24:54 +0000 (UTC)
Date: Sat, 13 Mar 2021 00:24:53 +0100
From: Kevin Wolf <kevin@tyndur.org>
To: tyndur-Patches <tyndur-devel@tyndur.org>
Subject: Re: [PATCH v2 2/2] =?iso-8859-1?Q?libc=3A_?=
 =?iso-8859-1?Q?=DCberarbeiten_des_=DCberlaufverhalten?= von scanf
Message-ID: <20210312232452.GB5262@faunus.fritz.box>
References: <20210304194839.64983-1-mayerNico256@gmail.com>
 <20210304194839.64983-2-mayerNico256@gmail.com>
Errors-To: toni@tyndur.org
Precedence: list
Reply-To: tyndur-Patches <tyndur-devel@tyndur.org>
List-Id: tyndur-Patches <tyndur-devel.tyndur.org>
List-Unsubscribe: <mailto:tyndur-devel+unsubscribe@tyndur.org>
List-Post: <mailto:tyndur-devel@tyndur.org>
List-Help: <mailto:tyndur-devel+help@tyndur.org>
List-Subscribe: <mailto:tyndur-devel+subscribe@tyndur.org>
List-Owner: <mailto:toni@tyndur.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <20210304194839.64983-2-mayerNico256@gmail.com>
User-Agent: Mutt/1.10.1 (2018-07-13)

Am 04.03.2021 um 20:48 hat Nico Mayer geschrieben:
> From: Nico Mayer <mayerNico@outlook.de>
> 
> ! Die bisherige Implementierung der Scanf-Funktion verwendet für das
>   Einlesen der Zahlen ausschließlich die Funktion strtoull. Hierdurch verhält
>   sich die Scanf-Funktion bei Überläufen bzw. Unterläufen bei verschiedenen
>   Datentypen falsch. Mit diesem Commit wählt jetzt die Scanf-Funktion
>   für den jeweiligen Datentyp die passende strtoxxx-Funktion aus.
> 
> Signed-off-by: Nico Mayer <mayerNico@outlook.de>
> ---
>  src/modules/lib/stdlibc/scanf.c | 26 +++++++++++++++++++++++++-
>  1 file changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/src/modules/lib/stdlibc/scanf.c b/src/modules/lib/stdlibc/scanf.c
> index 0f978606..6d288e36 100644
> --- a/src/modules/lib/stdlibc/scanf.c
> +++ b/src/modules/lib/stdlibc/scanf.c
> @@ -33,6 +33,7 @@
>  #include <stddef.h>
>  #include <string.h>
>  #include <ctype.h>
> +#include <stdbool.h>
>  
>  typedef int (*jscanf_getc)(void* state);
>  typedef void (*jscanf_ungetc)(void* state, char c);
> @@ -185,6 +186,7 @@ static int jscanf(const char* fmt, va_list ap,
>      int c;
>      char* endptr;
>      uint64_t value;
> +    bool is_unsigned = false;
>  
>      // 64 Bit oktal = 22 Zeichen, führende 0, Vorzeichen und \0
>      char buf[25];
> @@ -282,22 +284,30 @@ static int jscanf(const char* fmt, va_list ap,
>                      // %p - Pointer (strtoul mit Basis 16)
>                      case 'i':
>                          base = 0;
> +                        is_unsigned = false;
>                          goto convert_number;
>                      case 'o':
>                          base = 8;
> +                        is_unsigned = true;
>                          goto convert_number;
>                      case 'x':
>                      case 'X':
>                          base = 16;
> +                        is_unsigned = true;
>                          goto convert_number;
>                      case 'p':
>                          base = 16;
>                          size = sizeof(void*);
>                          len = 0;
> +                        is_unsigned = true;
>                          goto convert_number;
>                      case 'd':
> +                        base = 10;
> +                        is_unsigned = false;
> +                        goto convert_number;
>                      case 'u':
>                          base = 10;
> +                        is_unsigned = true;
>                      convert_number:
>                          if (len == 0 || len >= sizeof(buf)) {
>                              len = sizeof(buf) - 1;
> @@ -305,7 +315,21 @@ static int jscanf(const char* fmt, va_list ap,
>                          len = jscanf_read_number(buf, len, jgetc, jungetc,
>                              state, base, &eof);
>  
> -                        value = strtoull(buf, &endptr, base);
> +                        // Auswählen der richtigen strtoxxx-Funktion
> +                        if (is_unsigned) {
> +                            if (size == sizeof(int)) {
> +                                value = strtoul(buf, &endptr, base);
> +                            } else {
> +                                value = strtoull(buf, &endptr, base);
> +                            }
> +                        } else {
> +                            if (size == sizeof(int)) {
> +                                value = strtol(buf, &endptr, base);
> +                            } else {
> +                                value = strtoll(buf, &endptr, base);
> +                            }
> +                        }

Ok, den Unterschied zwischen signed und unsigned verstehe ich.

Aber den Unterschied zwischen strto(u)l und strto(u)ll nicht: Auf i386
benutzt du strtol() für int und long, aber strtoll() nicht nur für
long long, sondern auch für char und short.

Bist du sicher, dass das die Überläufe richtig macht?

>                          if ((endptr != buf + len) || (len == 0)) {
>                              if ((len == 0) && eof) {
>                                  goto input_error;

Kevin
