From patchwork Sat Jul 15 17:57:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 4324 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp2638497vsb; Sat, 15 Jul 2017 10:57:53 -0700 (PDT) X-Received: by 10.28.51.11 with SMTP id z11mr1755829wmz.115.1500141473570; Sat, 15 Jul 2017 10:57:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500141473; cv=none; d=google.com; s=arc-20160816; b=b1AV2kwO7IKid4mRYJRtD4+isEMJh4fyE4DEIaH4ESGwRFtf6wbFn8J67IbcZIUKtP jInIPwHT7rA9QyDsMS4Tw0PuRtYIeHRdH1xZsKGiGPu93h8OKVrES5jzicGtFdbZFQwS fLHS5cDNKMxdE7gba8JFKUqsrp+sozKw8ONqnLrrd5o7VFyq5f+1yFJwO9LmNifvJIb5 JIChMfuMWCb1oduZxDYqBSwNTMFg6jz+FlK6gPHm2a35ysxClSX0BdshyppYA/0GD+B0 JBj60WXCOpfguuyr2cz2vdM/0SgKq79P7g4pbEwEQcAqLxzxR2U2kiwshNNhIi16dGTb h2wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:delivered-to :arc-authentication-results; bh=C8KFdMp8qfC0idlMThOhyQ5bzA+IFbPyco+9xpxUa68=; b=E0nYU5rTpjcojM3YXT78teh1dEVm/4MR/u7DBK3/bw3OPanW08aYANlYBEouok/wGv tPeeXGTNaWjGrmznwcUdJ49j6texOJf2KAnFx4by+n0190da/RWquyGDncCa0ashI4u3 UWyOQGkfeOnKIqWKj1H1cPzH0/+7QMZdmY2tk8YBhTa76/JDRdeIKYBpgfVg11r9atSk gQ5cMPPZoknpFs5ek6jnTHPNW2irSNMonUJZJL80Elp1ZKXKkQgPeOb3cZpfPHkJB442 AgzBd/KMaI5MerZ229slIyKRGQsoyHeelhd/Ne0XuU9HhcsJMXm1GTVBF2sQJaOFzTbD rx1g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t205si5479631wmf.187.2017.07.15.10.57.53; Sat, 15 Jul 2017 10:57:53 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 55A1B6899D3; Sat, 15 Jul 2017 20:57:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-qmta-pe01-2.mx.upcmail.net (vie01a-qmta-pe01-2.mx.upcmail.net [62.179.121.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7DAE56806AF for ; Sat, 15 Jul 2017 20:57:38 +0300 (EEST) Received: from [172.31.218.52] (helo=vie01a-dmta-pe05-1.mx.upcmail.net) by vie01a-pqmta-pe01.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1dWRK4-00073q-5q for ffmpeg-devel@ffmpeg.org; Sat, 15 Jul 2017 19:57:44 +0200 Received: from [172.31.216.43] (helo=vie01a-pemc-psmtp-pe01) by vie01a-dmta-pe08.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1dWRJy-0004eG-Ek for ffmpeg-devel@ffmpeg.org; Sat, 15 Jul 2017 19:57:38 +0200 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe01 with SMTP @ mailcloud.upcmail.net id l5xU1v00J0S5wYM015xVTS; Sat, 15 Jul 2017 19:57:29 +0200 X-SourceIP: 213.47.41.20 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Sat, 15 Jul 2017 19:57:27 +0200 Message-Id: <20170715175727.11060-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.13.0 Subject: [FFmpeg-devel] [PATCH] doc: Add initial documentation explaining undefined behavior and SUINT X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Requested-by: Kieran Kunhya Signed-off-by: Michael Niedermayer --- doc/undefined.txt | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 doc/undefined.txt diff --git a/doc/undefined.txt b/doc/undefined.txt new file mode 100644 index 0000000000..957db3e2a9 --- /dev/null +++ b/doc/undefined.txt @@ -0,0 +1,47 @@ +Undefined Behavior +------------------ +In the C language, some operations are undefined, like signed integer overflow +dereferencing freed pointers, accessing outside allocated space, ... + +Undefined Behavior must not occur in a C program, it is not safe even if the +output of undefined operations is unused. The unsaftey may seem nit picking +but Optimizing compilers have in fact optimized code on the assumtation that +no undefined Behavior occurs. +Optimizing code based on wrong assumtations can and has in some cases lead to +effects beyond the output of computations. + + +The signed integer overflow problem in speed critical code +---------------------------------------------------------- +Code which is highly optimized and works with signed integers sometimes has the +problem that some (invalid) inputs can trigger overflows (undefined behavior). +In these cases, often the output of the computation does not matter (as it is +from invalid input). +In some cases the input can be checked easily in others checking the input is +computationally too intensive. +In these remaining cases a unsigned type can be used instead of a signed type. +unsigned overflows are defined in C. + +SUINT +----- +As we have above established there is a need to use "unsigned" sometimes in +computations which work with signed integers (which overflow). +Using "unsigned" for signed integers has the very significant potential to +cause confusion +as in +unsigned a,b,c; +... +a+b*c; +The reader does not expect b to be semantically -5 here and if the code is +changed by maybe adding a cast, a division or other the signeness will almost +certainly be mistaken. +To avoid this confusion a new type was introduced, "SUINT" is the C "unsigned" +type but it holds a signed "int". +to use the same example +SUINT a,b,c; +... +a+b*c; +here the reader knows that a,b,c are meant to be signed integers but for C +standard compliance / to avoid undefined behavior they are stored in unsigned +ints. +