[parted-devel] new error from gcc warns about ugly code

Jim Meyering jim at meyering.net
Mon Jan 14 19:08:19 UTC 2008


I built parted using gcc built from the latest sources and
was surprised to see a new error:

  traverse.c: In function 'fat_dir_entry_get_name':
  traverse.c:357: error: array subscript is above array bounds

Date: Mon, 14 Jan 2008 20:01:39 +0100
Subject: [PATCH] Avoid new error detected by very latest gcc.

* libparted/fs/fat/traverse.c (fat_dir_entry_get_name): Don't reference
->extension[3] via a pointer into the prior ->name[8] struct member.
gcc detected the reference beyond end of name[8].
Declare first parameter to be "const".
* libparted/fs/fat/traverse.c: Update prototype.

Signed-off-by: Jim Meyering <meyering at redhat.com>
---
 libparted/fs/fat/traverse.c |   18 ++++++++++--------
 libparted/fs/fat/traverse.h |    4 ++--
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/libparted/fs/fat/traverse.c b/libparted/fs/fat/traverse.c
index 3d2e2b5..367f511 100644
--- a/libparted/fs/fat/traverse.c
+++ b/libparted/fs/fat/traverse.c
@@ -1,6 +1,6 @@
 /*
     libparted
-    Copyright (C) 1998, 1999, 2000, 2005, 2007 Free Software Foundation, Inc.
+    Copyright (C) 1998-2000, 2005, 2007-2008 Free Software Foundation, Inc.

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -340,22 +340,24 @@ fat_dir_entry_has_first_cluster (FatDirEntry* dir_entry, PedFileSystem* fs)
     decrypts silly DOS names to FILENAME.EXT
 */
 void
-fat_dir_entry_get_name (FatDirEntry*dir_entry, char *result) {
+fat_dir_entry_get_name (const FatDirEntry *dir_entry, char *result) {
 	int     i;
-	char   *src;
+	const char *src;
+	const char *ext;

 	src = dir_entry->name;

-	for (i=0; i<8; i++) {
+	for (i=0; i < sizeof dir_entry->name; i++) {
 		if (src[i] == ' ' || src[i] == 0) break;
 		*result++ = src[i];
 	}

-	if (src[8] != ' ' && src[8] != 0) {
+	ext = (const char *) dir_entry->extension;
+	if (ext[0] != ' ' && ext[0] != 0) {
 		*result++ = '.';
-		for (i=8; i<11; i++) {
-			if (src[i] == ' ' || src[i] == 0) break;
-			*result++ = src[i];
+		for (i=0; i < sizeof dir_entry->extension; i++) {
+			if (ext[i] == ' ' || ext[i] == 0) break;
+			*result++ = ext[i];
 		}
 	}

diff --git a/libparted/fs/fat/traverse.h b/libparted/fs/fat/traverse.h
index 21e4c27..17e4580 100644
--- a/libparted/fs/fat/traverse.h
+++ b/libparted/fs/fat/traverse.h
@@ -1,6 +1,6 @@
 /*
     libparted
-    Copyright (C) 1998, 1999, 2000, 2007 Free Software Foundation, Inc.
+    Copyright (C) 1998, 1999, 2000, 2007-2008 Free Software Foundation, Inc.

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -65,7 +65,7 @@ extern int fat_dir_entry_is_null_term (const FatDirEntry* dir_entry);
 extern int fat_dir_entry_is_file (FatDirEntry* dir_entry);
 extern int fat_dir_entry_is_system_file (FatDirEntry* dir_entry);
 extern int fat_dir_entry_is_directory (FatDirEntry* dir_entry);
-extern void fat_dir_entry_get_name (FatDirEntry* dir_entry, char* result);
+extern void fat_dir_entry_get_name (const FatDirEntry* dir_entry, char* result);
 extern int fat_dir_entry_is_active (FatDirEntry* dir_entry);
 extern int fat_dir_entry_has_first_cluster (FatDirEntry* dir_entry,
 					    PedFileSystem* fs);
--
1.5.4.rc3.5.gac41



More information about the parted-devel mailing list