From patchwork Thu Jul 21 12:36:36 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: alexandros.frantzis@linaro.org X-Patchwork-Id: 2929 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id BF5EE23F4D for ; Thu, 21 Jul 2011 12:41:50 +0000 (UTC) Received: from mail-qw0-f52.google.com (mail-qw0-f52.google.com [209.85.216.52]) by fiordland.canonical.com (Postfix) with ESMTP id 8DCFAA1822A for ; Thu, 21 Jul 2011 12:41:50 +0000 (UTC) Received: by mail-qw0-f52.google.com with SMTP id 8so853237qwb.11 for ; Thu, 21 Jul 2011 05:41:50 -0700 (PDT) Received: by 10.229.217.3 with SMTP id hk3mr207932qcb.38.1311252110328; Thu, 21 Jul 2011 05:41:50 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.229.217.78 with SMTP id hl14cs139464qcb; Thu, 21 Jul 2011 05:41:50 -0700 (PDT) Received: by 10.216.132.214 with SMTP id o64mr163399wei.75.1311251797607; Thu, 21 Jul 2011 05:36:37 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id p71si2406265weq.6.2011.07.21.05.36.37; Thu, 21 Jul 2011 05:36:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) client-ip=91.189.90.139; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) smtp.mail=bounces@canonical.com Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QjsUW-0000DF-Pr for ; Thu, 21 Jul 2011 12:36:36 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id C2A222E889C for ; Thu, 21 Jul 2011 12:36:36 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: glmark2 X-Launchpad-Branch: ~glmark2-dev/glmark2/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 92 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~glmark2-dev/glmark2/trunk] Rev 92: Improve robustness of 3ds model loading. Message-Id: <20110721123636.17019.12252.launchpad@loganberry.canonical.com> Date: Thu, 21 Jul 2011 12:36:36 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13475"; Instance="initZopeless config overlay" X-Launchpad-Hash: 6ed097395d433d282e148f3ac4c432e26ec1f471 ------------------------------------------------------------ revno: 92 committer: Alexandros Frantzis timestamp: Fri 2011-06-24 12:24:40 +0300 message: Improve robustness of 3ds model loading. modified: src/model.cpp --- lp:glmark2 https://code.launchpad.net/~glmark2-dev/glmark2/trunk You are subscribed to branch lp:glmark2. To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription === modified file 'src/model.cpp' --- src/model.cpp 2011-06-21 12:38:07 +0000 +++ src/model.cpp 2011-06-24 09:24:40 +0000 @@ -119,6 +119,16 @@ mVertex[i].v *= pAmount; } +#define fread_or_fail(a, b, c, d) do { \ + size_t nread_; \ + nread_ = fread((a), (b), (c), (d));\ + if (nread_ < (c)) { \ + Log::error("Failed to read %zd bytes from 3ds file (read %zd)\n", \ + (size_t)((c) * (b)), nread_ * (b)); \ + return 0; \ + } \ +} while(0); + int Model::load_3ds(const char *pFileName) { int i; @@ -127,7 +137,6 @@ unsigned int l_chunk_length; unsigned char l_char; unsigned short l_qty; - size_t nread; Log::debug("Loading model from 3ds file '%s'\n", pFileName); @@ -139,9 +148,9 @@ // Loop to scan the whole file while (ftell (l_file) < filelength (fileno (l_file))) { // Read the chunk header - nread = fread (&l_chunk_id, 2, 1, l_file); + fread_or_fail (&l_chunk_id, 2, 1, l_file); //Read the lenght of the chunk - nread = fread (&l_chunk_length, 4, 1, l_file); + fread_or_fail (&l_chunk_length, 4, 1, l_file); switch (l_chunk_id) { @@ -169,7 +178,7 @@ case 0x4000: i = 0; do { - nread = fread (&l_char, 1, 1, l_file); + fread_or_fail (&l_char, 1, 1, l_file); mName[i] = l_char; i++; } while(l_char != '\0' && i<20); @@ -191,12 +200,12 @@ // + sub chunks //------------------------------------------- case 0x4110: - nread = fread (&l_qty, sizeof (unsigned short), 1, l_file); + fread_or_fail (&l_qty, sizeof (unsigned short), 1, l_file); mVertexQty = l_qty; mVertex = new Vertex[mVertexQty]; for (i = 0; i < l_qty; i++) { float f[3]; - nread = fread (f, sizeof(float), 3, l_file); + fread_or_fail (f, sizeof(float), 3, l_file); mVertex[i].v.x(f[0]); mVertex[i].v.y(f[1]); mVertex[i].v.z(f[2]); @@ -211,14 +220,14 @@ // + sub chunks //------------------------------------------- case 0x4120: - nread = fread (&l_qty, sizeof (unsigned short), 1, l_file); + fread_or_fail (&l_qty, sizeof (unsigned short), 1, l_file); mPolygonQty = l_qty; mPolygon = new Polygon[mPolygonQty]; for (i = 0; i < l_qty; i++) { - nread = fread (&mPolygon[i].mA, sizeof (unsigned short), 1, l_file); - nread = fread (&mPolygon[i].mB, sizeof (unsigned short), 1, l_file); - nread = fread (&mPolygon[i].mC, sizeof (unsigned short), 1, l_file); - nread = fread (&mPolygon[i].mFaceFlags, sizeof (unsigned short), 1, l_file); + fread_or_fail (&mPolygon[i].mA, sizeof (unsigned short), 1, l_file); + fread_or_fail (&mPolygon[i].mB, sizeof (unsigned short), 1, l_file); + fread_or_fail (&mPolygon[i].mC, sizeof (unsigned short), 1, l_file); + fread_or_fail (&mPolygon[i].mFaceFlags, sizeof (unsigned short), 1, l_file); } break; @@ -230,10 +239,10 @@ // + sub chunks //------------------------------------------- case 0x4140: - nread = fread (&l_qty, sizeof (unsigned short), 1, l_file); + fread_or_fail (&l_qty, sizeof (unsigned short), 1, l_file); for (i = 0; i < l_qty; i++) { float f[2]; - nread = fread (f, sizeof(float), 2, l_file); + fread_or_fail (f, sizeof(float), 2, l_file); mVertex[i].t.x(f[0]); mVertex[i].t.y(f[1]); }