9front - general discussion about 9front
 help / color / mirror / Atom feed
* dossrv: Some error checking.
@ 2020-04-06  0:48 ori
  2020-04-06  1:27 ` [9front] " Alex Musolino
  0 siblings, 1 reply; 4+ messages in thread
From: ori @ 2020-04-06  0:48 UTC (permalink / raw)
  To: 9front

This change adds some missing checks to dossrv. There
were a few places where we didn't check the return value
of getsect and getosect. This should catch all of them
and propagate the error upwards. I tested this lightly,
but the only fat partitions I have are the 9fat partions
I use for booting -- so if there are any heavy dossrv users,
I'd appreciate reports.

This came up last night when debugging sl's issues
when writing to large disks using dossrv.

diff -r ed9a8f50c08f sys/src/cmd/dossrv/dosfs.c
--- a/sys/src/cmd/dossrv/dosfs.c	Sun Apr 05 03:46:47 2020 +0200
+++ b/sys/src/cmd/dossrv/dosfs.c	Sun Apr 05 17:44:54 2020 -0700
@@ -550,7 +550,7 @@
 /*
  * wipe out a dos directory entry
  */
-static void
+static int
 doremove(Xfs *xf, Dosptr *dp)
 {
 	Iosect *p;
@@ -565,6 +565,8 @@
 	}
 	if(prevdo < 0 && dp->prevaddr != -1){
 		p = getsect(xf, dp->prevaddr);
+		if(p == nil)
+			return -1;
 		for(prevdo = ((Dosbpb*)xf->ptr)->sectsize-DOSDIRSIZE; prevdo >= 0; prevdo -= DOSDIRSIZE){
 			if(p->iobuf[prevdo+11] != 0xf)
 				break;
@@ -572,7 +574,8 @@
 			p->flags |= BMOD;
 		}
 		putsect(p);
-	}		
+	}
+	return 0;		
 }
 
 void
@@ -601,8 +604,7 @@
 	 * or it's a read only file in the root directory
 	 */
 	parp = getsect(f->xf, dp->paddr);
-	if(parp == nil
-	|| getfile(f) < 0){
+	if(parp == nil || getfile(f) < 0){
 		errno = Eio;
 		goto out;
 	}
@@ -617,7 +619,10 @@
 		errno = Eio;
 		goto out;
 	}
-	doremove(f->xf, f->ptr);
+	if(doremove(f->xf, f->ptr) == -1){
+		errno = Eio;
+		goto out;
+	}
 	if(!isroot(dp->paddr)){
 		puttime(pard, 0);
 		parp->flags |= BMOD;
diff -r ed9a8f50c08f sys/src/cmd/dossrv/dossubs.c
--- a/sys/src/cmd/dossrv/dossubs.c	Sun Apr 05 03:46:47 2020 +0200
+++ b/sys/src/cmd/dossrv/dossubs.c	Sun Apr 05 17:44:54 2020 -0700
@@ -1000,6 +1000,8 @@
 			c = count;
 		if(c == bp->sectsize){
 			p = getosect(xf, addr);
+			if(p == nil)
+				return -1;
 			p->flags = 0;
 		}else{
 			p = getsect(xf, addr);
@@ -1632,7 +1634,8 @@
 			if(rp == nil)
 				return -1;
 			wp = getosect(xf, ws);
-			assert(wp != nil);
+			if(wp == nil)
+				return -1;
 			memmove(wp->iobuf, rp->iobuf, bp->sectsize);
 			wp->flags = BMOD;
 			putsect(rp);
@@ -1683,8 +1686,7 @@
 {
 	Dosbpb *bp = xf->ptr;
 	Iosect *p;
-	int n, i;
-	vlong k;
+	int n, i, k;
 
 	n = bp->freeptr;
 	for(;;){
@@ -1702,6 +1704,8 @@
 	k = clust2sect(bp, n);
 	for(i=0; i<bp->clustsize; i++){
 		p = getosect(xf, k+i);
+		if(p == nil)
+			return -1;
 		memset(p->iobuf, 0, bp->sectsize);
 		p->flags = BMOD;
 		putsect(p);
diff -r ed9a8f50c08f sys/src/cmd/dossrv/iotrack.c
--- a/sys/src/cmd/dossrv/iotrack.c	Sun Apr 05 03:46:47 2020 +0200
+++ b/sys/src/cmd/dossrv/iotrack.c	Sun Apr 05 17:44:54 2020 -0700
@@ -39,8 +39,10 @@
 	int toff;
 	Iosect *p;
 
-	if(addr < 0)
+	if(addr < 0){
+		werrstr("invalid address");
 		return nil;
+	}
 	toff = addr % Sect2trk;
 	taddr = addr - toff;
 	t = getiotrack(xf, taddr);
@@ -53,8 +55,10 @@
 	}
 	t->ref++;
 	p = t->tp->p[toff];
-	if(p == 0){
+	if(p == nil){
 		p = newsect();
+		if(p == nil)
+			return nil;
 		t->tp->p[toff] = p;
 		p->flags = t->flags&BSTALE;
 		p->lock.key = 0;
@@ -196,10 +200,10 @@
 			t->flags &= ~BSTALE;
 	}
 	if(devwrite(t->xf, t->addr, t->tp->buf, Trksize) < 0){
-		chat("error]");
+		chat("error]\n");
 		return -1;
 	}
-	chat(" done]");
+	chat(" done]\n");
 	return 0;
 }
 
@@ -304,6 +308,8 @@
 	else
 		p = malloc(sizeof(Iosect));
 	unmlock(&freelock);
+	if(p == nil)
+		return nil;
 	p->next = 0;
 	return p;
 }



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9front] dossrv: Some error checking.
  2020-04-06  0:48 dossrv: Some error checking ori
@ 2020-04-06  1:27 ` Alex Musolino
  2020-04-06  1:39   ` ori
  2020-04-06  2:02   ` ori
  0 siblings, 2 replies; 4+ messages in thread
From: Alex Musolino @ 2020-04-06  1:27 UTC (permalink / raw)
  To: 9front

> @@ -1683,8 +1686,7 @@
>  {
>  	Dosbpb *bp = xf->ptr;
>  	Iosect *p;
> -	int n, i;
> -	vlong k;
> +	int n, i, k;
>  
>  	n = bp->freeptr;
>  	for(;;){

Is this part intentional?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9front] dossrv: Some error checking.
  2020-04-06  1:27 ` [9front] " Alex Musolino
@ 2020-04-06  1:39   ` ori
  2020-04-06  2:02   ` ori
  1 sibling, 0 replies; 4+ messages in thread
From: ori @ 2020-04-06  1:39 UTC (permalink / raw)
  To: alex, 9front

>> @@ -1683,8 +1686,7 @@
>>  {
>>  	Dosbpb *bp = xf->ptr;
>>  	Iosect *p;
>> -	int n, i;
>> -	vlong k;
>> +	int n, i, k;
>>  
>>  	n = bp->freeptr;
>>  	for(;;){
> 
> Is this part intentional?

...No, it definitely isn't. Good catch.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9front] dossrv: Some error checking.
  2020-04-06  1:27 ` [9front] " Alex Musolino
  2020-04-06  1:39   ` ori
@ 2020-04-06  2:02   ` ori
  1 sibling, 0 replies; 4+ messages in thread
From: ori @ 2020-04-06  2:02 UTC (permalink / raw)
  To: alex, 9front

>> @@ -1683,8 +1686,7 @@
>>  {
>>  	Dosbpb *bp = xf->ptr;
>>  	Iosect *p;
>> -	int n, i;
>> -	vlong k;
>> +	int n, i, k;
>>  
>>  	n = bp->freeptr;
>>  	for(;;){
> 
> Is this part intentional?

Now with the merge issue fixed, and errno set instead of werrstr:

diff -r ed9a8f50c08f sys/src/cmd/dossrv/dosfs.c
--- a/sys/src/cmd/dossrv/dosfs.c	Sun Apr 05 03:46:47 2020 +0200
+++ b/sys/src/cmd/dossrv/dosfs.c	Sun Apr 05 19:01:41 2020 -0700
@@ -550,7 +550,7 @@
 /*
  * wipe out a dos directory entry
  */
-static void
+static int
 doremove(Xfs *xf, Dosptr *dp)
 {
 	Iosect *p;
@@ -565,6 +565,8 @@
 	}
 	if(prevdo < 0 && dp->prevaddr != -1){
 		p = getsect(xf, dp->prevaddr);
+		if(p == nil)
+			return -1;
 		for(prevdo = ((Dosbpb*)xf->ptr)->sectsize-DOSDIRSIZE; prevdo >= 0; prevdo -= DOSDIRSIZE){
 			if(p->iobuf[prevdo+11] != 0xf)
 				break;
@@ -572,7 +574,8 @@
 			p->flags |= BMOD;
 		}
 		putsect(p);
-	}		
+	}
+	return 0;		
 }
 
 void
@@ -601,8 +604,7 @@
 	 * or it's a read only file in the root directory
 	 */
 	parp = getsect(f->xf, dp->paddr);
-	if(parp == nil
-	|| getfile(f) < 0){
+	if(parp == nil || getfile(f) < 0){
 		errno = Eio;
 		goto out;
 	}
@@ -617,7 +619,10 @@
 		errno = Eio;
 		goto out;
 	}
-	doremove(f->xf, f->ptr);
+	if(doremove(f->xf, f->ptr) == -1){
+		errno = Eio;
+		goto out;
+	}
 	if(!isroot(dp->paddr)){
 		puttime(pard, 0);
 		parp->flags |= BMOD;
diff -r ed9a8f50c08f sys/src/cmd/dossrv/dossubs.c
--- a/sys/src/cmd/dossrv/dossubs.c	Sun Apr 05 03:46:47 2020 +0200
+++ b/sys/src/cmd/dossrv/dossubs.c	Sun Apr 05 19:01:41 2020 -0700
@@ -1000,6 +1000,8 @@
 			c = count;
 		if(c == bp->sectsize){
 			p = getosect(xf, addr);
+			if(p == nil)
+				return -1;
 			p->flags = 0;
 		}else{
 			p = getsect(xf, addr);
@@ -1632,7 +1634,8 @@
 			if(rp == nil)
 				return -1;
 			wp = getosect(xf, ws);
-			assert(wp != nil);
+			if(wp == nil)
+				return -1;
 			memmove(wp->iobuf, rp->iobuf, bp->sectsize);
 			wp->flags = BMOD;
 			putsect(rp);
@@ -1702,6 +1705,8 @@
 	k = clust2sect(bp, n);
 	for(i=0; i<bp->clustsize; i++){
 		p = getosect(xf, k+i);
+		if(p == nil)
+			return -1;
 		memset(p->iobuf, 0, bp->sectsize);
 		p->flags = BMOD;
 		putsect(p);
diff -r ed9a8f50c08f sys/src/cmd/dossrv/iotrack.c
--- a/sys/src/cmd/dossrv/iotrack.c	Sun Apr 05 03:46:47 2020 +0200
+++ b/sys/src/cmd/dossrv/iotrack.c	Sun Apr 05 19:01:41 2020 -0700
@@ -39,8 +39,11 @@
 	int toff;
 	Iosect *p;
 
-	if(addr < 0)
+	if(addr < 0){
+		chat("invalid address\n");
+		errno = Eio;
 		return nil;
+	}
 	toff = addr % Sect2trk;
 	taddr = addr - toff;
 	t = getiotrack(xf, taddr);
@@ -53,8 +56,10 @@
 	}
 	t->ref++;
 	p = t->tp->p[toff];
-	if(p == 0){
+	if(p == nil){
 		p = newsect();
+		if(p == nil)
+			return nil;
 		t->tp->p[toff] = p;
 		p->flags = t->flags&BSTALE;
 		p->lock.key = 0;
@@ -196,10 +201,10 @@
 			t->flags &= ~BSTALE;
 	}
 	if(devwrite(t->xf, t->addr, t->tp->buf, Trksize) < 0){
-		chat("error]");
+		chat("error]\n");
 		return -1;
 	}
-	chat(" done]");
+	chat(" done]\n");
 	return 0;
 }
 
@@ -304,6 +309,8 @@
 	else
 		p = malloc(sizeof(Iosect));
 	unmlock(&freelock);
+	if(p == nil)
+		return nil;
 	p->next = 0;
 	return p;
 }



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-04-06  2:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-06  0:48 dossrv: Some error checking ori
2020-04-06  1:27 ` [9front] " Alex Musolino
2020-04-06  1:39   ` ori
2020-04-06  2:02   ` ori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).