From patchwork Fri Jun 24 09:57:14 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: 2291 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 387D823F08 for ; Fri, 24 Jun 2011 09:57:20 +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 DDCCAA184DC for ; Fri, 24 Jun 2011 09:57:19 +0000 (UTC) Received: by qwb8 with SMTP id 8so1958030qwb.11 for ; Fri, 24 Jun 2011 02:57:19 -0700 (PDT) Received: by 10.229.40.139 with SMTP id k11mr2350328qce.135.1308909438266; Fri, 24 Jun 2011 02:57:18 -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.230.139 with SMTP id jm11cs44520qcb; Fri, 24 Jun 2011 02:57:17 -0700 (PDT) Received: by 10.216.233.211 with SMTP id p61mr2692315weq.107.1308909435250; Fri, 24 Jun 2011 02:57:15 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id x58si5744251wec.61.2011.06.24.02.57.14; Fri, 24 Jun 2011 02:57:15 -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 1Qa38U-0008Ij-Eh for ; Fri, 24 Jun 2011 09:57:14 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 6AD172E8134 for ; Fri, 24 Jun 2011 09:57:14 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: glmark2 X-Launchpad-Branch: ~afrantzis/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 ~afrantzis/glmark2/trunk] Rev 92: Improve robustness of 3ds model loading. Message-Id: <20110624095714.11861.48714.launchpad@loganberry.canonical.com> Date: Fri, 24 Jun 2011 09:57:14 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13265"; Instance="initZopeless config overlay" X-Launchpad-Hash: 960157f24112ac41035916e6aeb8507fea5b71c8 ------------------------------------------------------------ revno: 92 committer: Alexandros Frantzis branch nick: trunk 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/~afrantzis/glmark2/trunk You are subscribed to branch lp:glmark2. To unsubscribe from this branch go to https://code.launchpad.net/~afrantzis/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]); }