Dear all,

Here is the updated patch against trunk which checks the return values
of various IO operations.

Reviews, comments are welcome.

Please consider applying this to trunk or tell me why not.

-- 
With regards,

Dmitrijs Ledkovs
=== modified file 'src/utilfuns/zlib/untgz.c'
--- src/utilfuns/zlib/untgz.c	2001-03-23 09:00:15 +0000
+++ src/utilfuns/zlib/untgz.c	2012-03-22 19:32:07 +0000
@@ -13,6 +13,8 @@
 #include <fcntl.h>
 #ifdef unix
 # include <unistd.h>
+# include <sys/stat.h>
+# include <sys/types.h>
 #else
 # include <direct.h>
 # include <io.h>
@@ -277,7 +279,7 @@
 	int    remaining = 0;
 	FILE   *outfile = NULL;
 	char   fname[BLOCKSIZE];
-	time_t tartime;
+	time_t tartime = 0;
   
 	while (1) {
 		len = gzread(in, &buffer, BLOCKSIZE);

=== modified file 'tests/testblocks.cpp'
--- tests/testblocks.cpp	2009-02-07 08:42:00 +0000
+++ tests/testblocks.cpp	2012-03-22 19:32:07 +0000
@@ -29,14 +29,18 @@
 void addEntry(EntriesBlock *eb) {
 	string input;
 	string body;
-	char line[1024];
+	char line[1024], *c;
 	std::cout << "\nEnter new Entry's text. '.' on an empty line to finish:\n";
 	do {
 		std::cout << "> ";
-		fgets(line, 1000, stdin);
-		input = line;
-		if (input.compare("."))
-			body.append(input);
+		c = fgets(line, 1000, stdin);
+		if (c == NULL) {
+			std::cerr << "ERROR: fgets failed during addEntry\n";
+		} else {
+			input = line;
+			if (input.compare("."))
+				body.append(input);
+		}
 	}
 	while (input.compare("."));
 	std::cout << "Adding new entry.  Index is: " << eb->addEntry(body.c_str()) << "\n\n";
@@ -72,29 +76,33 @@
 
 	EntriesBlock *eb = new EntriesBlock();
 	string input;
-	char line[1024];
+	char line[1024], *c;
 
 	std::cout << "Initial entry count should be 0: " << eb->getCount() << "\n";
 
 	do {
 		std::cout << "[" << eb->getCount() << "] > ";
-		fgets(line, 1000, stdin);
-		input = line;
-		if (input.length() > 0) {
-			switch (input[0]) {
-				case 'a': addEntry(eb); break;
-				case 'p':	printEntry(eb, atoi(input.c_str()+1)); break;
-				case 'r':	removeEntry(eb, atoi(input.c_str()+1)); break;
-				case 's': printSize(eb); break;
-				case 'q': break;
-				case '?':
-				default:
-					std::cout << "\n a - add a new entry\n";
-					std::cout << " p <entry_index> - print entry\n";
-					std::cout << " r <entry_index> - remove entry\n";
-					std::cout << " s - print size of raw data\n";
-					std::cout << " q - quit\n\n";
-					break;
+		c = fgets(line, 1000, stdin);
+		if (c == NULL) {
+			std::cerr << "ERROR: fgets failed in main\n";
+		} else {
+			input = line;
+			if (input.length() > 0) {
+				switch (input[0]) {
+					case 'a': addEntry(eb); break;
+					case 'p':	printEntry(eb, atoi(input.c_str()+1)); break;
+					case 'r':	removeEntry(eb, atoi(input.c_str()+1)); break;
+					case 's': printSize(eb); break;
+					case 'q': break;
+					case '?':
+					default:
+						std::cout << "\n a - add a new entry\n";
+						std::cout << " p <entry_index> - print entry\n";
+						std::cout << " r <entry_index> - remove entry\n";
+						std::cout << " s - print size of raw data\n";
+						std::cout << " q - quit\n\n";
+						break;
+				}
 			}
 		}
 	}

=== modified file 'utilities/cipherraw.cpp'
--- utilities/cipherraw.cpp	2011-07-23 17:39:20 +0000
+++ utilities/cipherraw.cpp	2012-03-22 19:32:07 +0000
@@ -48,7 +48,7 @@
 	SWCipher *zobj;
 	VerseKey key;
 	RawVerse *rawdrv;
-	int ofd[2], oxfd[2];
+	int ofd[2], oxfd[2], w;
 	long tmpoff = 0, offset, loffset = 0, lzoffset = 0;
 	unsigned short size, lsize = 0, lzsize;
 	char *tmpbuf;
@@ -74,10 +74,18 @@
 	delete [] tmpbuf;
 
 	printf("\n");
-	write(oxfd[0], &lzoffset, 4);
-	write(oxfd[0], &lzsize, 2);
-	write(oxfd[1], &lzoffset, 4);
-	write(oxfd[1], &lzsize, 2);
+	w = write(oxfd[0], &lzoffset, 4);
+	if (w < 0)
+		perror("ERROR: write failed in main");
+	w = write(oxfd[0], &lzsize, 2);
+	if (w < 0)
+		perror("ERROR: write failed in main");
+	w = write(oxfd[1], &lzoffset, 4);
+	if (w < 0)
+		perror("ERROR: write failed in main");
+	w = write(oxfd[1], &lzsize, 2);
+	if (w < 0)
+		perror("ERROR: write failed in main");
 
 	key.AutoNormalize(0);
 	key.Headings(1);
@@ -89,8 +97,13 @@
 			printf("using previous offset,size %d\n", size);
 			offset = lseek(oxfd[key.Testament() - 1], 0, SEEK_CUR);
 			printf("%ld %ld %d \n", offset, lzoffset, lzsize);
-			write(oxfd[key.Testament() - 1], &lzoffset, 4);
-			write(oxfd[key.Testament() - 1], &lzsize, 2);
+			w = write(oxfd[key.Testament() - 1], &lzoffset, 4);
+			if (w < 0)
+				perror("ERROR: write failed in main");
+
+			w = write(oxfd[key.Testament() - 1], &lzsize, 2);
+			if (w < 0)
+				perror("ERROR: write failed in main");
 		}
 		else {
 			lsize   = size;
@@ -107,13 +120,23 @@
 			offset = lseek(ofd[key.Testament() - 1], 0, SEEK_CUR);
 			tmpoff = lseek(oxfd[key.Testament() - 1], 0, SEEK_CUR);
 			printf("%s: (%ld) NEW offset: %ld; size: %d\n", (const char *)key, tmpoff, offset, size);
-			write(oxfd[key.Testament() - 1], &offset, 4);
+			w = write(oxfd[key.Testament() - 1], &offset, 4);
+			if (w < 0)
+				perror("ERROR: write failed in main");
+
 			unsigned long ulSize = size;
-			if (size) 
-				write(ofd[key.Testament() - 1], zobj->cipherBuf(&ulSize), size);
+			if (size) {
+				w = write(ofd[key.Testament() - 1], zobj->cipherBuf(&ulSize), size);
+				if (w < 0)
+					perror("ERROR: write failed in main");
+			}
+
 			size = (unsigned int)ulSize;
 			lzoffset = offset;
-			write(oxfd[key.Testament() - 1], &size, 2);
+			w = write(oxfd[key.Testament() - 1], &size, 2);
+			if (w < 0)
+				perror("ERROR: write failed in main");
+
 			lzsize = size;
 		}
 	}

=== modified file 'utilities/gbfidx.cpp'
--- utilities/gbfidx.cpp	2009-02-07 08:24:08 +0000
+++ utilities/gbfidx.cpp	2012-03-22 19:32:07 +0000
@@ -53,7 +53,7 @@
 int main(int argc, char **argv)
 {
 	long pos, offset;
-	int num1, num2, rangemax;
+	int num1, num2, rangemax, w;
 	char startflag = 0;
 	short size;
 
@@ -65,18 +65,30 @@
 	num1 = key1.Chapter();
 	num2 = key1.Verse();
 	pos  = 0;
-	write(bfp, &pos, 4);  /* Book    offset for testament intros */
+	w = write(bfp, &pos, 4);  /* Book    offset for testament intros */
+	if (w != 4)
+		perror("ERROR: write failed in main");
 	pos = 4;
-	write(cfp, &pos, 4);  /* Chapter offset for testament intro */
+	w = write(cfp, &pos, 4);  /* Chapter offset for testament intro */
+	if (w != 4)
+		perror("ERROR: write failed in main");
 
 
 /*	Right now just zero out intros until parsing correctly */
 	pos = 0;
 	size = 0;
-	write(vfp, &pos, 4);  /* Module intro */
-	write(vfp, &size, 2);
-	write(vfp, &pos, 4);  /* Testament intro */
-	write(vfp, &size, 2);
+	w = write(vfp, &pos, 4);  /* Module intro */
+	if (w != 4)
+		perror("ERROR: write failed in main");
+	w = write(vfp, &size, 2);
+	if (w != 2)
+		perror("ERROR: write failed in main");
+	w = write(vfp, &pos, 4);  /* Testament intro */
+	if (w != 4)
+		perror("ERROR: write failed in main");
+	w = write(vfp, &size, 2);
+	if (w != 2)
+		perror("ERROR: write failed in main");
 
 	while(!findbreak(fp, &offset, &num1, &num2, &rangemax, &size)) {
 		if (!startflag) {
@@ -120,6 +132,7 @@
 
 void writeidx(VerseKey &key1, VerseKey &key2, VerseKey &key3, long offset, short size)
 {
+	int w;
 	long pos;
 	short tmp;
 
@@ -127,26 +140,48 @@
 		if (key1.Verse() == 1) {	// new chapter
 			if (key1.Chapter() == 1) {	// new book
 				pos = lseek(cfp, 0, SEEK_CUR);
-				write(bfp, &pos, 4);
+				w = write(bfp, &pos, 4);
+				if (w != 2)
+					perror("ERROR: write failed in writeidx");
 				pos = lseek(vfp, 0, SEEK_CUR); /* Book intro (cps) */
-				write(cfp, &pos, 4);
-				write(vfp, &chapoffset, 4);  /* Book intro (vss)  set to same as chap for now(it should be chap 1 which usually contains the book into anyway)*/
-				write(vfp, &chapsize, 2);
+				w = write(cfp, &pos, 4);
+				if (w != 2)
+					perror("ERROR: write failed in writeidx");
+				w = write(vfp, &chapoffset, 4);  /* Book intro (vss)  set to same as chap for now(it should be chap 1 which usually contains the book into anyway)*/
+				if (w != 2)
+					perror("ERROR: write failed in writeidx");
+				w = write(vfp, &chapsize, 2);
+				if (w != 2)
+					perror("ERROR: write failed in writeidx");
 			}
 			pos = lseek(vfp, 0, SEEK_CUR);
-			write(cfp, &pos, 4);
-			write(vfp, &chapoffset, 4);  /* Chapter intro */
-			write(vfp, &chapsize, 2);
+			w = write(cfp, &pos, 4);
+			if (w != 4)
+				perror("ERROR: write failed in writeidx");
+			w = write(vfp, &chapoffset, 4);  /* Chapter intro */
+			if (w != 4)
+				perror("ERROR: write failed in writeidx");
+			w = write(vfp, &chapsize, 2);
+			if (w != 2)
+				perror("ERROR: write failed in writeidx");
 		}
 		if (key1 >= key2) {
-			write(vfp, &offset, 4);
-			write(vfp, &size, 2);
+			w = write(vfp, &offset, 4);
+			if (w != 4)
+				perror("ERROR: write failed in writeidx");
+			w = write(vfp, &size, 2);
+			if (w != 2)
+				perror("ERROR: write failed in writeidx");
 		}
 		else	{
 			pos = 0;
 			tmp = 0;
-			write(vfp, &pos, 4);
-			write(vfp, &tmp, 2);
+			w = write(vfp, &pos, 4);
+			if (w != 4)
+				perror("ERROR: write failed in writeidx");
+			w = write(vfp, &tmp, 2);
+			if (w != 2)
+				perror("ERROR: write failed in writeidx");
 		}
 	}
 }

=== modified file 'utilities/genbookutil.cpp'
--- utilities/genbookutil.cpp	2010-10-04 08:35:09 +0000
+++ utilities/genbookutil.cpp	2012-03-22 19:32:07 +0000
@@ -53,9 +53,11 @@
 
 
 void setLocalName(TreeKeyIdx *treeKey) {
-	char buf[1023];
+  char buf[1023], *c;
 	std::cout << "Enter New Node Name: ";
-	fgets(buf, 1000, stdin);
+	c = fgets(buf, 1000, stdin);
+	if (c == NULL)
+		std::cerr << "ERROR: fgets failed in setLocalName\n";
 	SWBuf name = buf;
 	treeKey->setLocalName(name.trim());
 	treeKey->save();
@@ -63,18 +65,22 @@
 
 
 void gotoPath(TreeKeyIdx *treeKey) {
-	char buf[1023];
+  char buf[1023], *c;
 	std::cout << "Enter Path: ";
-	fgets(buf, 1000, stdin);
+	c = fgets(buf, 1000, stdin);
+	if (c == NULL)
+		std::cerr << "ERROR: fgets failed in gotoPath\n";
 	SWBuf path = buf;
 	(*treeKey) = path.trim();
 }
 
 
 void assurePath(TreeKeyIdx *treeKey) {
-	char buf[1023];
+  char buf[1023], *c;
 	std::cout << "Enter Path: ";
-	fgets(buf, 1000, stdin);
+	c = fgets(buf, 1000, stdin);
+	if (c == NULL)
+		std::cerr << "ERROR: fgets failed in assurePath\n";
 	SWBuf path = buf;
 	treeKey->assureKeyPath(path.trim());
 }
@@ -91,10 +97,12 @@
 	SWBuf body;
 	TreeKeyIdx *treeKey = (TreeKeyIdx *)(SWKey *)(*book);
 	if (treeKey->getOffset()) {
-		char buf[1023];
+	  char buf[1023], *c;
 		std::cout << "Enter New Entry Text ('.' on a line by itself to end): \n";
 		do {
-			fgets(buf, 1000, stdin);
+			c = fgets(buf, 1000, stdin);
+			if (c == NULL)
+				std::cerr << "ERROR: fgets failed in setEntryText\n";
 			SWBuf text = buf;
 			text.trim();
 			if ((text[0] == '.') && (text[1] == 0))
@@ -111,9 +119,11 @@
 
 void appendSibbling(TreeKeyIdx *treeKey) {
 	if (treeKey->getOffset()) {
-		char buf[1023];
+	  char buf[1023], *c;
 		std::cout << "Enter New Sibbling Name: ";
-		fgets(buf, 1000, stdin);
+		c = fgets(buf, 1000, stdin);
+		if (c == NULL)
+			std::cerr << "ERROR: fgets failed in appendSibbling\n";
 		SWBuf name = buf;
 		treeKey->append();
 		treeKey->setLocalName(name.trim());
@@ -124,9 +134,11 @@
 
 
 void appendChild(TreeKeyIdx *treeKey) {
-	char buf[1023];
+  char buf[1023], *c;
 	std::cout << "Enter New Child Name: ";
-	fgets(buf, 1000, stdin);
+	c = fgets(buf, 1000, stdin);
+	if (c == NULL)
+		std::cerr << "ERROR: fgets failed in appendChild\n";
 	SWBuf name = buf;
 	treeKey->appendChild();
 	treeKey->setLocalName(name.trim());
@@ -168,11 +180,13 @@
 	treeKey = (TreeKeyIdx *)(SWKey *)(*book);
 
 	SWBuf input;
-	char line[1024];
+	char line[1024], *c;
 
 	do {
 		std::cout << "[" << treeKey->getText() << "] > ";
-		fgets(line, 1000, stdin);
+		c = fgets(line, 1000, stdin);
+		if (c == NULL)
+			std::cerr << "ERROR: fgets failed in main\n";
 		input = line;
 		input.trim();
 		if (input.length() > 0) {

=== modified file 'utilities/installmgr.cpp'
--- utilities/installmgr.cpp	2010-11-06 17:53:46 +0000
+++ utilities/installmgr.cpp	2012-03-22 19:32:07 +0000
@@ -69,8 +69,10 @@
 		cout << "then type yes at the prompt\n\n";
 		cout << "enable? [no] ";
 
-		char prompt[10];
-		fgets(prompt, 9, stdin);
+		char prompt[10], *c;
+		c = fgets(prompt, 9, stdin);
+		if (c == NULL)
+			std::cerr <<"ERROR: fgets failed in isUserDisclaimerConfirmed\n";
 		confirmed = (!strcmp(prompt, "yes\n"));
 		cout << "\n";
 	}

=== modified file 'utilities/lexdump.c'
--- utilities/lexdump.c	2009-04-29 06:21:35 +0000
+++ utilities/lexdump.c	2012-03-22 19:32:07 +0000
@@ -40,7 +40,7 @@
 
 int main(int argc, char **argv) {
 	char *tmpbuf;
-	int idxfd, datfd;
+	int idxfd, datfd, r;
 	long offset;
 	unsigned int size;
 	char datbuf[255];
@@ -59,11 +59,17 @@
 
 	offset = atoi(argv[2]) * 6;
 	lseek(idxfd, offset, SEEK_SET);
-	read(idxfd, &offset, 4);
-	read(idxfd, &size, 2);
+	r = read(idxfd, &offset, 4);
+	if (r != 4)
+		perror("ERROR: read failed in main");
+	r = read(idxfd, &size, 2);
+	if (r != 2)
+		perror("ERROR: read failed in main");
 	printf("offset: %ld; size: %d\n", offset, size);
 	lseek(datfd, offset, SEEK_SET);
-	read(datfd, datbuf, 40);
+	r = read(datfd, datbuf, 40);
+	if (r != 40)
+		perror("ERROR: read failed in main");
 	datbuf[40] = 0;
 	printf("%s\n", datbuf);
 	close(datfd);

=== modified file 'utilities/step2vpl.cpp'
--- utilities/step2vpl.cpp	2010-10-04 08:35:09 +0000
+++ utilities/step2vpl.cpp	2012-03-22 19:32:07 +0000
@@ -223,93 +223,169 @@
 
 
 void readVersion(int fd, Version *versionRecord) {
-
-	read(fd, &(versionRecord->versionRecordSize), 2);
-	read(fd, &(versionRecord->publisherID), 2);
-	read(fd, &(versionRecord->bookID), 2);
-	read(fd, &(versionRecord->setID), 2);
-	read(fd, &(versionRecord->conversionProgramVerMajor), 1);
-	read(fd, &(versionRecord->conversionProgramVerMinor), 1);
-	read(fd, &(versionRecord->leastCompatSTEPVerMajor), 1);
-	read(fd, &(versionRecord->leastCompatSTEPVerMinor), 1);
-	read(fd, &(versionRecord->encryptionType), 1);
-	read(fd, &(versionRecord->editionID), 1);
-	read(fd, &(versionRecord->modifiedBy), 2);
+	int r;
+
+	r = read(fd, &(versionRecord->versionRecordSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
+
+	r = read(fd, &(versionRecord->publisherID), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->bookID), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->setID), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->conversionProgramVerMajor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->conversionProgramVerMinor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->leastCompatSTEPVerMajor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->leastCompatSTEPVerMinor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->encryptionType), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->editionID), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
+	r = read(fd, &(versionRecord->modifiedBy), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
 
 	int skip = versionRecord->versionRecordSize - 16/*sizeof(struct Version*/;
 
 	if (skip) {
 		char *skipbuf = new char[skip];
-		read(fd, skipbuf, skip);
+		r = read(fd, skipbuf, skip);
+		if (r != skip)
+			perror("ERROR: read failed in readVersion");
 		delete [] skipbuf;
 	}
 }
 
 
 void readSectionsHeader(int fd, SectionsHeader *sectionsHeaderRecord) {
+	int r;
 
-	read(fd, &(sectionsHeaderRecord->sectionsHeaderRecordSize), 2);
-	read(fd, &(sectionsHeaderRecord->levelEntriesCount), 4);
-	read(fd, &(sectionsHeaderRecord->glossEntriesCount), 4);
-	read(fd, &(sectionsHeaderRecord->levelEntriesSize), 2);
-	read(fd, &(sectionsHeaderRecord->reserved), 4);
+	r = read(fd, &(sectionsHeaderRecord->sectionsHeaderRecordSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readSectionsHeader");
+	r = read(fd, &(sectionsHeaderRecord->levelEntriesCount), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionsHeader");
+	r = read(fd, &(sectionsHeaderRecord->glossEntriesCount), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionsHeader");
+	r = read(fd, &(sectionsHeaderRecord->levelEntriesSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readSectionsHeader");
+	r = read(fd, &(sectionsHeaderRecord->reserved), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionsHeader");
 
 	int skip = sectionsHeaderRecord->sectionsHeaderRecordSize - 16/*sizeof(struct ViewableHeader)*/;
 
 	if (skip) {
 		char *skipbuf = new char[skip];
-		read(fd, skipbuf, skip);
+		r = read(fd, skipbuf, skip);
+		if (r != skip)
+			perror("ERROR: read failed in readSectionsHeader");
 		delete [] skipbuf;
 	}
 }
 
 
 void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord) {
+	int r;
 
-	read(fd, &(viewableHeaderRecord->viewableHeaderRecordSize), 2);
-	read(fd, &(viewableHeaderRecord->viewableBlocksCount), 4);
-	read(fd, &(viewableHeaderRecord->glossBlocksCount), 4);
-	read(fd, &(viewableHeaderRecord->compressionType), 1);
-	read(fd, &(viewableHeaderRecord->reserved1), 1);
-	read(fd, &(viewableHeaderRecord->blockEntriesSize), 2);
-	read(fd, &(viewableHeaderRecord->reserved2), 2);
+	r = read(fd, &(viewableHeaderRecord->viewableHeaderRecordSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readViewableHeader");
+	r = read(fd, &(viewableHeaderRecord->viewableBlocksCount), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableHeader");
+	r = read(fd, &(viewableHeaderRecord->glossBlocksCount), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableHeader");
+	r = read(fd, &(viewableHeaderRecord->compressionType), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readViewableHeader");
+	r = read(fd, &(viewableHeaderRecord->reserved1), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readViewableHeader");
+	r = read(fd, &(viewableHeaderRecord->blockEntriesSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readViewableHeader");
+	r = read(fd, &(viewableHeaderRecord->reserved2), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readViewableHeader");
 
 	int skip = viewableHeaderRecord->viewableHeaderRecordSize - 16/*sizeof(struct ViewableHeader)*/;
 
 	if (skip) {
 		char *skipbuf = new char[skip];
-		read(fd, skipbuf, skip);
+		r = read(fd, skipbuf, skip);
+		if (r != skip)
+			perror("ERROR: read failed in readViewableHeader");
 		delete [] skipbuf;
 	}
 }
 
 
 void readVSyncHeader(int fd, VSyncHeader *vSyncHeaderRecord) {
+	int r;
 
-	read(fd, &(vSyncHeaderRecord->vSyncHeaderRecordSize), 2);
-	read(fd, &(vSyncHeaderRecord->startBookNumber), 2);
-	read(fd, &(vSyncHeaderRecord->endBookNumber), 2);
-	read(fd, &(vSyncHeaderRecord->bookPointerEntriesSize), 2);
-	read(fd, &(vSyncHeaderRecord->syncPointEntriesSize), 2);
-	read(fd, &(vSyncHeaderRecord->reserved1_1), 4);
-	read(fd, &(vSyncHeaderRecord->reserved1_2), 2);
+	r = read(fd, &(vSyncHeaderRecord->vSyncHeaderRecordSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVSyncHeader");
+	r = read(fd, &(vSyncHeaderRecord->startBookNumber), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVSyncHeader");
+	r = read(fd, &(vSyncHeaderRecord->endBookNumber), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVSyncHeader");
+	r = read(fd, &(vSyncHeaderRecord->bookPointerEntriesSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVSyncHeader");
+	r = read(fd, &(vSyncHeaderRecord->syncPointEntriesSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVSyncHeader");
+	r = read(fd, &(vSyncHeaderRecord->reserved1_1), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readVSyncHeader");
+	r = read(fd, &(vSyncHeaderRecord->reserved1_2), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVSyncHeader");
 
 	int skip = vSyncHeaderRecord->vSyncHeaderRecordSize - 16/*sizeof(VSyncHeader)*/;
 
 	if (skip) {
 		char *skipbuf = new char[skip];
-		read(fd, skipbuf, skip);
+		r = read(fd, skipbuf, skip);
+		if (r != skip)
+			perror("ERROR: read failed in readVSyncHeader");
 		delete [] skipbuf;
 	}
 }
 
 
 void readViewableBlockText(int fd, ViewableBlock *vb, char **buf) {
+	int r;
 	unsigned long size = vb->size;
 
 	*buf = new char [ ((vb->size > vb->uncompressedSize) ? vb->size : vb->uncompressedSize) + 1 ];
 	lseek(fd, vb->offset, SEEK_SET);
-	read(fd, *buf, vb->size);
+	r = read(fd, *buf, vb->size);
+	if (r != vb->size)
+		perror("ERROR: read failed in readViewableBlockText");
 
 	compress->zBuf(&size, *buf);
 	strcpy(*buf, compress->Buf());
@@ -317,35 +393,53 @@
 
 
 void readViewableBlock(int fd, ViewableBlock *vb) {
+	int r;
 
-	read(fd, &(vb->offset), 4);
-	read(fd, &(vb->uncompressedSize), 4);
-	read(fd, &(vb->size), 4);
+	r = read(fd, &(vb->offset), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableBlock");
+	r = read(fd, &(vb->uncompressedSize), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableBlock");
+	r = read(fd, &(vb->size), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableBlock");
 }
 
 
 void readHeaderControlWordAreaText(int fd, char **buf) {
+	int r;
 	long headerControlWordAreaSize;
-	read(fd, &headerControlWordAreaSize, 4);
+	r = read(fd, &headerControlWordAreaSize, 4);
+	if (r != 4)
+		perror("ERROR: read failed in readHeaderControlWordAreaText");
 
 	*buf = new char [headerControlWordAreaSize + 1];
 
-	read(fd, *buf, headerControlWordAreaSize);
+	r = read(fd, *buf, headerControlWordAreaSize);
+	if (r != headerControlWordAreaSize)
+		perror("ERROR: read failed in readHeaderControlWordAreaText");
 	(*buf)[headerControlWordAreaSize] = 0;
 
 }
 
 void readVSyncBooksInfo(int fd, VSyncHeader *vSyncHeaderRecord, VSyncBooksInfo **vSyncBooksInfo) {
 
+	int r;
 	int bookCount = vSyncHeaderRecord->endBookNumber - vSyncHeaderRecord->startBookNumber;
 	*vSyncBooksInfo = new VSyncBooksInfo[bookCount];
 	for (int i = 0; i <= bookCount; i++) {
-		read(fd, &(*vSyncBooksInfo)[i].offset, 4);
-		read(fd, &(*vSyncBooksInfo)[i].count, 2);
+		r = read(fd, &(*vSyncBooksInfo)[i].offset, 4);
+		if (r != 4)
+			perror("ERROR: read failed in readVSyncBooksInfo");
+		r = read(fd, &(*vSyncBooksInfo)[i].count, 2);
+		if (r != 2)
+			perror("ERROR: read failed in readVSyncBooksInfo");
 	}
 }
 
 void displayBook(int fdbook, int fdviewable, int fdvsync, int fdsections, VSyncBooksInfo *vSyncBooksInfo) {
+	int r;
 	VSyncPoint vSyncPoint;
 
 	lseek(fdvsync, vSyncBooksInfo->offset, SEEK_SET);
@@ -356,9 +450,15 @@
 		char *sectionName;
 		char *verseText;
 
-		read(fdvsync, &(vSyncPoint.chapter), 2);
-		read(fdvsync, &(vSyncPoint.verse), 2);
-		read(fdvsync, &(vSyncPoint.offset), 4);
+		r = read(fdvsync, &(vSyncPoint.chapter), 2);
+		if (r != 2)
+			perror("ERROR: read failed in displayBook");
+		r = read(fdvsync, &(vSyncPoint.verse), 2);
+		if (r != 2)
+			perror("ERROR: read failed in displayBook");
+		r = read(fdvsync, &(vSyncPoint.offset), 4);
+		if (r != 4)
+			perror("ERROR: read failed in displayBook");
 		vSyncPoint.offset = SECTIONSLEVELSTART + (vSyncPoint.offset * SECTIONSLEVELSIZE);
 		lseek(fdsections, vSyncPoint.offset, SEEK_SET);
 		readSectionLevelInfo(fdsections, &sectionLevelInfo);
@@ -413,26 +513,50 @@
 
 
 void readSectionName(int fd, SectionLevelInfo *sli, char **name) {
+	int r;
 	short size;
 	lseek(fd, sli->nameOffset, SEEK_SET);
-	read(fd, &size, 2);
+	r = read(fd, &size, 2);
+	if (r != 2)
+		perror("ERROR: read failed in readSectionName");
 	*name = new char [ size + 1 ];
-	read(fd, *name, size);
+	r = read(fd, *name, size);
+	if (r != size)
+		perror("ERROR: read failed in readSectionName");
 	(*name)[size] = 0;
 }
 
 void readSectionLevelInfo(int fd, SectionLevelInfo *sli) {
+	int r;
 
-	read(fd, &(sli->parentOffset), 4);
-	read(fd, &(sli->previousOffset), 4);
-	read(fd, &(sli->nextOffset), 4);
-	read(fd, &(sli->viewableOffset), 4);
+	r = read(fd, &(sli->parentOffset), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionLevelInfo");
+	r = read(fd, &(sli->previousOffset), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionLevelInfo");
+	r = read(fd, &(sli->nextOffset), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionLevelInfo");
+	r = read(fd, &(sli->viewableOffset), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionLevelInfo");
 	sli->viewableOffset = VIEWABLEBLOCKSTART + (VIEWABLEBLOCKSIZE * sli->viewableOffset);
-	read(fd, &(sli->startLevel), 2);
-	read(fd, &(sli->level), 1);
-	read(fd, &(sli->nameOffset), 4);
-	read(fd, &(sli->outSync_1), 4);
-	read(fd, &(sli->outSync_2), 2);
+	r = read(fd, &(sli->startLevel), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readSectionLevelInfo");
+	r = read(fd, &(sli->level), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readSectionLevelInfo");
+	r = read(fd, &(sli->nameOffset), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionLevelInfo");
+	r = read(fd, &(sli->outSync_1), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readSectionLevelInfo");
+	r = read(fd, &(sli->outSync_2), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readSectionLevelInfo");
 }
 
 void cleanBuf(char *buf) {

=== modified file 'utilities/stepdump.cpp'
--- utilities/stepdump.cpp	2010-10-04 08:35:09 +0000
+++ utilities/stepdump.cpp	2012-03-22 19:32:07 +0000
@@ -158,33 +158,56 @@
 
 
 void readVersion(int fd, Version *versionRecord) {
+	int r;
 
 	cout << "\n\nReading Version Record (" << 16/*sizeof(struct Version)*/ << " bytes)\n\n";
 // DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
 //	read(fd, &versionRecord, sizeof(struct Version));
 
 	cout << "Version Record Information\n";
-	read(fd, &(versionRecord->versionRecordSize), 2);
+	r = read(fd, &(versionRecord->versionRecordSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tversionRecordSize: " << versionRecord->versionRecordSize << "\n";
-	read(fd, &(versionRecord->publisherID), 2);
+	r = read(fd, &(versionRecord->publisherID), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tpublisherID: " << versionRecord->publisherID << "\n";
-	read(fd, &(versionRecord->bookID), 2);
+	r = read(fd, &(versionRecord->bookID), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tbookID: " << versionRecord->bookID << "\n";
-	read(fd, &(versionRecord->setID), 2);
+	r = read(fd, &(versionRecord->setID), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tsetID: " << versionRecord->setID << "\n";
-	read(fd, &(versionRecord->conversionProgramVerMajor), 1);
+	r = read(fd, &(versionRecord->conversionProgramVerMajor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tconversionProgramVerMajor: " << (int)versionRecord->conversionProgramVerMajor << "\n";
-	read(fd, &(versionRecord->conversionProgramVerMinor), 1);
+	r = read(fd, &(versionRecord->conversionProgramVerMinor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tconversionProgramVerMinor: " << (int)versionRecord->conversionProgramVerMinor << "\n";
-	read(fd, &(versionRecord->leastCompatSTEPVerMajor), 1);
+	r = read(fd, &(versionRecord->leastCompatSTEPVerMajor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tleastCompatSTEPVerMajor: " << (int)versionRecord->leastCompatSTEPVerMajor << "\n";
-	read(fd, &(versionRecord->leastCompatSTEPVerMinor), 1);
+	r = read(fd, &(versionRecord->leastCompatSTEPVerMinor), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tleastCompatSTEPVerMinor: " << (int)versionRecord->leastCompatSTEPVerMinor << "\n";
-	read(fd, &(versionRecord->encryptionType), 1);
+	r = read(fd, &(versionRecord->encryptionType), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tencryptionType: " << (int)versionRecord->encryptionType << "\n";
-	read(fd, &(versionRecord->editionID), 1);
+	r = read(fd, &(versionRecord->editionID), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readVersion");
 	cout << "\teditionID: " << (int)versionRecord->editionID << "\n";
-	read(fd, &(versionRecord->modifiedBy), 2);
+	r = read(fd, &(versionRecord->modifiedBy), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readVersion");
 	cout << "\tmodifiedBy: " << versionRecord->modifiedBy << "\n";
 
 	int skip = versionRecord->versionRecordSize - 16/*sizeof(struct Version*/;
@@ -192,33 +215,49 @@
 	if (skip) {
 		cout << "\nSkipping " << skip << " unknown bytes.\n";
 		char *skipbuf = new char[skip];
-		read(fd, skipbuf, skip);
+		r = read(fd, skipbuf, skip);
+		if (r != skip)
+			perror("ERROR: read failed in readVersion");
 		delete [] skipbuf;
 	}
 }
 
 
 void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord) {
-
+	int r;
 	cout << "\n\nReading Viewable Header Record (" << 16/*sizeof(struct ViewableHeader)*/ << " bytes)\n\n";
 
 // DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
 //	read(fd, &viewableHeaderRecord, sizeof(struct ViewableHeader));
 
 	cout << "Viewable Header Record Information\n";
-	read(fd, &(viewableHeaderRecord->viewableHeaderRecordSize), 2);
+	r = read(fd, &(viewableHeaderRecord->viewableHeaderRecordSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readViewableHeader");
 	cout << "\tviewableHeaderRecordSize: " << viewableHeaderRecord->viewableHeaderRecordSize << "\n";
-	read(fd, &(viewableHeaderRecord->viewableBlocksCount), 4);
+	r = read(fd, &(viewableHeaderRecord->viewableBlocksCount), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableHeader");
 	cout << "\tviewableBlocksCount: " << viewableHeaderRecord->viewableBlocksCount << "\n";
-	read(fd, &(viewableHeaderRecord->glossBlocksCount), 4);
+	r = read(fd, &(viewableHeaderRecord->glossBlocksCount), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableHeader");
 	cout << "\tglossBlocksCount: " << viewableHeaderRecord->glossBlocksCount << "\n";
-	read(fd, &(viewableHeaderRecord->compressionType), 1);
+	r = read(fd, &(viewableHeaderRecord->compressionType), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readViewableHeader");
 	cout << "\tcompressionType: " << (int)viewableHeaderRecord->compressionType << "(0 - none; 1 - LZSS)\n";
-	read(fd, &(viewableHeaderRecord->reserved1), 1);
+	r = read(fd, &(viewableHeaderRecord->reserved1), 1);
+	if (r != 1)
+		perror("ERROR: read failed in readViewableHeader");
 	cout << "\treserved1: " << (int)viewableHeaderRecord->reserved1 << "\n";
-	read(fd, &(viewableHeaderRecord->blockEntriesSize), 2);
+	r = read(fd, &(viewableHeaderRecord->blockEntriesSize), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readViewableHeader");
 	cout << "\tblockEntriesSize: " << viewableHeaderRecord->blockEntriesSize << "\n";
-	read(fd, &(viewableHeaderRecord->reserved2), 2);
+	r = read(fd, &(viewableHeaderRecord->reserved2), 2);
+	if (r != 2)
+		perror("ERROR: read failed in readViewableHeader");
 	cout << "\treserved2: " << viewableHeaderRecord->reserved2 << "\n";
 
 	int skip = viewableHeaderRecord->viewableHeaderRecordSize - 16/*sizeof(struct ViewableHeader)*/;
@@ -226,18 +265,23 @@
 	if (skip) {
 		cout << "\nSkipping " << skip << " unknown bytes.\n";
 		char *skipbuf = new char[skip];
-		read(fd, skipbuf, skip);
+		r = read(fd, skipbuf, skip);
+		if (r != skip)
+			perror("ERROR: read failed in readViewableHeader");
 		delete [] skipbuf;
 	}
 }
 
 
 void readViewableBlockText(int fd, ViewableBlock *vb, char **buf) {
+	int r;
 	unsigned long size = vb->size;
 
 	*buf = new char [ ((vb->size > vb->uncompressedSize) ? vb->size : vb->uncompressedSize) + 1 ];
 	lseek(fd, vb->offset, SEEK_SET);
-	read(fd, *buf, vb->size);
+	r = read(fd, *buf, vb->size);
+	if (r != vb->size)
+		perror("ERROR: read failed in readViewableBlocktext");
 
 	compress->zBuf(&size, *buf);
 	strcpy(*buf, compress->Buf());
@@ -247,30 +291,41 @@
 
 
 void readViewableBlock(int fd, ViewableBlock *vb) {
-
+	int r;
 	cout << "\n\nReading Viewable Block (" << 12/*sizeof(struct ViewableHeader)*/ << " bytes)\n\n";
 
 // DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
 //	read(fd, &vb, sizeof(struct ViewableBlock));
 
 	cout << "Viewable Block Information\n";
-	read(fd, &(vb->offset), 4);
+	r = read(fd, &(vb->offset), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableBlock");
 	cout << "\toffset: " << vb->offset << "\n";
-	read(fd, &(vb->uncompressedSize), 4);
+	r = read(fd, &(vb->uncompressedSize), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableBlock");
 	cout << "\tuncompressedSize: " << vb->uncompressedSize << "\n";
-	read(fd, &(vb->size), 4);
+	r = read(fd, &(vb->size), 4);
+	if (r != 4)
+		perror("ERROR: read failed in readViewableBlock");
 	cout << "\tsize: " << vb->size << "\n";
 }
 
 
 void readHeaderControlWordAreaText(int fd, char **buf) {
+	int r;
 	long headerControlWordAreaSize;
-	read(fd, &headerControlWordAreaSize, 4);
+	r = read(fd, &headerControlWordAreaSize, 4);
+	if (r != 4)
+		perror("ERROR: read failed in readHeaderControlWordAreaText");
 	cout << "Reading Header Control Word Area (" << headerControlWordAreaSize << " bytes)\n\n";
 
 	*buf = new char [headerControlWordAreaSize + 1];
 
-	read(fd, *buf, headerControlWordAreaSize);
+	r = read(fd, *buf, headerControlWordAreaSize);
+	if (r != headerControlWordAreaSize)
+		perror("ERROR: read failed in readHeaderControlWordAreaText");
 	(*buf)[headerControlWordAreaSize] = 0;
 
 	cout << "headerControlWordArea:\n" << *buf << "\n";

=== modified file 'utilities/treeidxutil.cpp'
--- utilities/treeidxutil.cpp	2010-10-04 08:35:09 +0000
+++ utilities/treeidxutil.cpp	2012-03-22 19:32:07 +0000
@@ -53,27 +53,33 @@
 
 
 void setLocalName(TreeKeyIdx *treeKey) {
-	char buf[1023];
+  char buf[1023], *c;
 	std::cout << "Enter New Node Name: ";
-	fgets(buf, 1000, stdin);
+	c = fgets(buf, 1000, stdin);
+	if (c == NULL)
+		std::cerr << "ERROR: fgets failed in setLocalName\n";
 	treeKey->setLocalName(buf);
 	treeKey->save();
 }
 
 
 void assurePath(TreeKeyIdx *treeKey) {
-	char buf[1023];
+  char buf[1023], *c;
 	std::cout << "Enter path: ";
-	fgets(buf, 1000, stdin);
+	c = fgets(buf, 1000, stdin);
+	if (c == NULL)
+		std::cerr << "ERROR: fgets failed in assurePath\n";
 	treeKey->assureKeyPath(buf);
 }
 
 
 void appendSibbling(TreeKeyIdx *treeKey) {
 	if (treeKey->getOffset()) {
-		char buf[1023];
+	  char buf[1023], *c;
 		std::cout << "Enter New Sibbling Name: ";
-		fgets(buf, 1000, stdin);
+		c = fgets(buf, 1000, stdin);
+		if (c == NULL)
+			std::cerr << "ERROR: fgets failed in appendSibbling\n";
 		treeKey->append();
 		treeKey->setLocalName(buf);
 		treeKey->save();
@@ -83,9 +89,11 @@
 
 
 void appendChild(TreeKeyIdx *treeKey) {
-	char buf[1023];
+  char buf[1023], *c;
 	std::cout << "Enter New Child Name: ";
-	fgets(buf, 1000, stdin);
+	c = fgets(buf, 1000, stdin);
+	if (c == NULL)
+		std::cerr << "ERROR: fgets failed in appendChild\n";
 	treeKey->appendChild();
 	treeKey->setLocalName(buf);
 	treeKey->save();
@@ -118,11 +126,13 @@
 	TreeKeyIdx root = *treeKey;
 
 	std::string input;
-	char line[1024];
+	char line[1024], *c;
 
 	do {
 		std::cout << "[" << treeKey->getText() << "] > ";
-		fgets(line, 1000, stdin);
+		c = fgets(line, 1000, stdin);
+		if (c == NULL)
+			std::cerr << "ERROR: fgets failed in main\n";
 		input = line;
 		if (input.length() > 0) {
 			switch (input[0]) {

=== modified file 'utilities/vpl2mod.cpp'
--- utilities/vpl2mod.cpp	2010-10-04 08:35:09 +0000
+++ utilities/vpl2mod.cpp	2012-03-22 19:32:07 +0000
@@ -72,14 +72,18 @@
 			break;
 	}
 	
-	int size = (lseek(fd, 0, SEEK_CUR) - index) - 1;
+	int r, size = (lseek(fd, 0, SEEK_CUR) - index) - 1;
 
 	*buf = new char [ size + 1 ];
 
 	if (size > 0) {
 		lseek(fd, index, SEEK_SET);
-		read(fd, *buf, size);
-		read(fd, &ch, 1);   //pop terminating char
+		r = read(fd, *buf, size);
+		if (r != size)
+			std::cerr <<"ERROR: short read in readline\n";
+		r = read(fd, &ch, 1);   //pop terminating char
+		if (r != size)
+			std::cerr <<"ERROR: short read of terminating char in readline\n";
 		(*buf)[size] = 0;
 
 		// clean up any trailing junk on buf

_______________________________________________
sword-devel mailing list: [email protected]
http://www.crosswire.org/mailman/listinfo/sword-devel
Instructions to unsubscribe/change your settings at above page

Reply via email to