- don't leak memory on allocation failure.

- s/perror/perr and print how much we were trying to allocate.
This commit is contained in:
christos 2006-06-05 16:51:18 +00:00
parent 42f4810aa0
commit 15f3040f9f
4 changed files with 53 additions and 37 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: boot.c,v 1.10 2005/01/19 20:00:45 xtraeme Exp $ */
/* $NetBSD: boot.c,v 1.11 2006/06/05 16:51:18 christos Exp $ */
/*
* Copyright (C) 1995, 1997 Wolfgang Solfrank
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: boot.c,v 1.10 2005/01/19 20:00:45 xtraeme Exp $");
__RCSID("$NetBSD: boot.c,v 1.11 2006/06/05 16:51:18 christos Exp $");
#endif /* not lint */
#include <stdlib.h>
@ -56,7 +56,7 @@ readboot(int dosfs, struct bootblock *boot)
int ret = FSOK;
if (read(dosfs, block, sizeof block) < sizeof block) {
perror("could not read boot block");
perr("could not read boot block");
return FSFATAL;
}
@ -108,7 +108,7 @@ readboot(int dosfs, struct bootblock *boot)
!= boot->FSInfo * boot->BytesPerSec
|| read(dosfs, fsinfo, sizeof fsinfo)
!= sizeof fsinfo) {
perror("could not read fsinfo block");
perr("could not read fsinfo block");
return FSFATAL;
}
if (memcmp(fsinfo, "RRaA", 4)
@ -135,7 +135,7 @@ readboot(int dosfs, struct bootblock *boot)
!= boot->FSInfo * boot->BytesPerSec
|| write(dosfs, fsinfo, sizeof fsinfo)
!= sizeof fsinfo) {
perror("Unable to write FSInfo");
perr("Unable to write FSInfo");
return FSFATAL;
}
ret = FSBOOTMOD;
@ -154,7 +154,7 @@ readboot(int dosfs, struct bootblock *boot)
if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
!= boot->Backup * boot->BytesPerSec
|| read(dosfs, backup, sizeof backup) != sizeof backup) {
perror("could not read backup bootblock");
perr("could not read backup bootblock");
return FSFATAL;
}
backup[65] = block[65]; /* XXX */
@ -235,7 +235,7 @@ writefsinfo(int dosfs, struct bootblock *boot)
if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
!= boot->FSInfo * boot->BytesPerSec
|| read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
perror("could not read fsinfo block");
perr("could not read fsinfo block");
return FSFATAL;
}
fsinfo[0x1e8] = (u_char)boot->FSFree;
@ -250,7 +250,7 @@ writefsinfo(int dosfs, struct bootblock *boot)
!= boot->FSInfo * boot->BytesPerSec
|| write(dosfs, fsinfo, sizeof fsinfo)
!= sizeof fsinfo) {
perror("Unable to write FSInfo");
perr("Unable to write FSInfo");
return FSFATAL;
}
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: check.c,v 1.13 2005/01/19 20:00:45 xtraeme Exp $ */
/* $NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $ */
/*
* Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: check.c,v 1.13 2005/01/19 20:00:45 xtraeme Exp $");
__RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $");
#endif /* not lint */
#include <stdlib.h>
@ -74,7 +74,7 @@ checkfilesys(const char *filename)
printf("\n");
if (dosfs < 0) {
perror("Can't open");
perr("Can't open `%s'", filename);
return 8;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: dir.c,v 1.19 2006/04/10 03:25:11 dbj Exp $ */
/* $NetBSD: dir.c,v 1.20 2006/06/05 16:51:18 christos Exp $ */
/*
* Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: dir.c,v 1.19 2006/04/10 03:25:11 dbj Exp $");
__RCSID("$NetBSD: dir.c,v 1.20 2006/06/05 16:51:18 christos Exp $");
#endif /* not lint */
#include <stdio.h>
@ -227,16 +227,29 @@ resetDosDirSection(struct bootblock *boot, struct fatEntry *fat)
int b1, b2;
cl_t cl;
int ret = FSOK;
size_t len;
b1 = boot->RootDirEnts * 32;
b2 = boot->SecPerClust * boot->BytesPerSec;
if (!(buffer = malloc(b1 > b2 ? b1 : b2))
|| !(delbuf = malloc(b2))
|| !(rootDir = newDosDirEntry())) {
perror("No space for directory");
if ((buffer = malloc(len = b1 > b2 ? b1 : b2)) == NULL) {
perr("No space for directory buffer (%zu)", len);
return FSFATAL;
}
if ((delbuf = malloc(len = b2)) == NULL) {
free(buffer);
perr("No space for directory delbuf (%zu)", len);
return FSFATAL;
}
if ((rootDir = newDosDirEntry()) == NULL) {
free(buffer);
free(delbuf);
perr("No space for directory entry");
return FSFATAL;
}
memset(rootDir, 0, sizeof *rootDir);
if (boot->flags & FAT32) {
if (boot->RootCl < CLUST_FIRST || boot->RootCl >= boot->NumClusters) {
@ -324,7 +337,7 @@ delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
off *= boot->BytesPerSec;
if (lseek(f, off, SEEK_SET) != off
|| read(f, delbuf, clsz) != clsz) {
perror("Unable to read directory");
perr("Unable to read directory");
return FSFATAL;
}
while (s < e) {
@ -333,7 +346,7 @@ delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
}
if (lseek(f, off, SEEK_SET) != off
|| write(f, delbuf, clsz) != clsz) {
perror("Unable to write directory");
perr("Unable to write directory");
return FSFATAL;
}
if (startcl == endcl)
@ -469,7 +482,7 @@ readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
off *= boot->BytesPerSec;
if (lseek(f, off, SEEK_SET) != off
|| read(f, buffer, last) != last) {
perror("Unable to read directory");
perr("Unable to read directory");
return FSFATAL;
}
last /= 32;
@ -815,7 +828,7 @@ readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
/* create directory tree node */
if (!(d = newDosDirEntry())) {
perror("No space for directory");
perr("No space for directory");
return FSFATAL;
}
memcpy(d, &dirent, sizeof(struct dosDirEntry));
@ -824,7 +837,7 @@ readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
/* Enter this directory into the todo list */
if (!(n = newDirTodo())) {
perror("No space for todo list");
perr("No space for todo list");
return FSFATAL;
}
n->next = pendingDirectories;
@ -845,7 +858,7 @@ readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
last *= 32;
if (lseek(f, off, SEEK_SET) != off
|| write(f, buffer, last) != last) {
perror("Unable to write directory");
perr("Unable to write directory");
return FSFATAL;
}
mod &= ~THISMOD;
@ -864,7 +877,7 @@ readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
last *= 32;
if (lseek(f, off, SEEK_SET) != off
|| write(f, buffer, last) != last) {
perror("Unable to write directory");
perr("Unable to write directory");
return FSFATAL;
}
mod &= ~THISMOD;
@ -935,7 +948,7 @@ reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
if (!lfbuf) {
lfbuf = malloc(boot->ClusterSize);
if (!lfbuf) {
perror("No space for buffer");
perr("No space for buffer");
return FSFATAL;
}
p = NULL;
@ -959,7 +972,7 @@ reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
+ boot->ClusterOffset * boot->BytesPerSec;
if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
|| read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
perror("could not read LOST.DIR");
perr("could not read LOST.DIR");
return FSFATAL;
}
p = lfbuf;
@ -989,7 +1002,7 @@ reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
fat[head].flags |= FAT_USED;
if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
|| write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
perror("could not write LOST.DIR");
perr("could not write LOST.DIR");
return FSFATAL;
}
return FSDIRMOD;

View File

@ -1,4 +1,4 @@
/* $NetBSD: fat.c,v 1.17 2006/03/20 01:42:47 christos Exp $ */
/* $NetBSD: fat.c,v 1.18 2006/06/05 16:51:18 christos Exp $ */
/*
* Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: fat.c,v 1.17 2006/03/20 01:42:47 christos Exp $");
__RCSID("$NetBSD: fat.c,v 1.18 2006/06/05 16:51:18 christos Exp $");
#endif /* not lint */
#include <stdlib.h>
@ -90,10 +90,11 @@ static int
_readfat(int fs, struct bootblock *boot, int no, u_char **buffer)
{
off_t off;
size_t len;
*buffer = malloc(boot->FATsecs * boot->BytesPerSec);
*buffer = malloc(len = boot->FATsecs * boot->BytesPerSec);
if (*buffer == NULL) {
perror("No space for FAT");
perr("No space for FAT sectors (%zu)", len);
return 0;
}
@ -101,13 +102,13 @@ _readfat(int fs, struct bootblock *boot, int no, u_char **buffer)
off *= boot->BytesPerSec;
if (lseek(fs, off, SEEK_SET) != off) {
perror("Unable to read FAT");
perr("Unable to read FAT");
goto err;
}
if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
!= boot->FATsecs * boot->BytesPerSec) {
perror("Unable to read FAT");
perr("Unable to read FAT");
goto err;
}
@ -128,18 +129,20 @@ readfat(int fs, struct bootblock *boot, int no, struct fatEntry **fp)
u_char *buffer, *p;
cl_t cl;
int ret = FSOK;
size_t len;
boot->NumFree = boot->NumBad = 0;
if (!_readfat(fs, boot, no, &buffer))
return FSFATAL;
fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
fat = malloc(len = boot->NumClusters * sizeof(struct fatEntry));
if (fat == NULL) {
perror("No space for FAT");
perr("No space for FAT clusters (%zu)", len);
free(buffer);
return FSFATAL;
}
(void)memset(fat, 0, len);
if (buffer[0] != boot->Media
|| buffer[1] != 0xff || buffer[2] != 0xff
@ -474,7 +477,7 @@ writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
if (buffer == NULL) {
perror("No space for FAT");
perr("No space for FAT sectors (%zu)", fatsz);
return FSFATAL;
}
memset(buffer, 0, fatsz);
@ -559,7 +562,7 @@ writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
off *= boot->BytesPerSec;
if (lseek(fs, off, SEEK_SET) != off
|| write(fs, buffer, fatsz) != fatsz) {
perror("Unable to write FAT");
perr("Unable to write FAT");
ret = FSFATAL; /* Return immediately? XXX */
}
}