Fix a segmentation fault in awk

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Fix a segmentation fault in awk

Frederic Cambus
Hi tech@,

Here is a diff to fix a segmentation fault in awk, from upstream
version 20121220 [1]. Upstream fix didn't check for strdup return value
so I added the check.

I've been seeing some awk.core files in my home directory for a while
now, and finally decided to try investigating why it happened. It turns
out misc/mc calls awk to process file information when navigating through
ZIP files, and it sometimes causes awk to segfault.

[1] http://distcache.FreeBSD.org/ports-distfiles/nawk-20121220/awk.tar.gz

Comments? OK?

Index: usr.bin/awk/run.c
===================================================================
RCS file: /cvs/src/usr.bin/awk/run.c,v
retrieving revision 1.42
diff -u -p -r1.42 run.c
--- usr.bin/awk/run.c 9 Oct 2017 14:51:31 -0000 1.42
+++ usr.bin/awk/run.c 12 Aug 2019 18:01:14 -0000
@@ -1217,13 +1217,15 @@ Cell *dopa2(Node **a, int n) /* a[0], a[
 Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
 {
  Cell *x = 0, *y, *ap;
- char *s;
+ char *s, *origs;
  int sep;
  char *t, temp, num[50], *fs = 0;
  int n, tempstat, arg3type;
 
  y = execute(a[0]); /* source string */
- s = getsval(y);
+ origs = s = strdup(getsval(y));
+ if (s == NULL)
+ FATAL("out of space in split");
  arg3type = ptoi(a[3]);
  if (a[2] == 0) /* fs string */
  fs = *FS;
@@ -1343,6 +1345,7 @@ Cell *split(Node **a, int nnn) /* split(
  }
  tempfree(ap);
  tempfree(y);
+ free(origs);
  if (a[2] != 0 && arg3type == STRING) {
  tempfree(x);
  }

Reply | Threaded
Open this post in threaded view
|

Re: Fix a segmentation fault in awk

Andras Farkas
On Mon, Aug 12, 2019 at 3:45 PM Frederic Cambus <[hidden email]> wrote:
> Hi tech@,
> Here is a diff to fix a segmentation fault in awk, from upstream
> version 20121220 [1]. Upstream fix didn't check for strdup return value
> so I added the check.
I've always been curious, why isn't the latest version of awk, the
2012 version, used in OpenBSD?  There may be a reason, but I've never
been able to figure it out.

Reply | Threaded
Open this post in threaded view
|

Re: Fix a segmentation fault in awk

Chris Cappuccio
Andras Farkas [[hidden email]] wrote:
> On Mon, Aug 12, 2019 at 3:45 PM Frederic Cambus <[hidden email]> wrote:
> > Hi tech@,
> > Here is a diff to fix a segmentation fault in awk, from upstream
> > version 20121220 [1]. Upstream fix didn't check for strdup return value
> > so I added the check.
> I've always been curious, why isn't the latest version of awk, the
> 2012 version, used in OpenBSD?  There may be a reason, but I've never
> been able to figure it out.

Ironically the last update was in 2011. Todd Miller has been keeping it up-to-
date until that point. It'd make sense for someone to analyze the local
changes and import the newer version. The only reason seems to be that the
work needs to be done. Importing the fix is a good first step.

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Fix a segmentation fault in awk

Chris Cappuccio
In reply to this post by Frederic Cambus
Frederic Cambus [[hidden email]] wrote:
>
> [1] http://distcache.FreeBSD.org/ports-distfiles/nawk-20121220/awk.tar.gz
>

Following the lack of hosting from Bell Labs, the post-2012 tree is on
Github.  Brian Kernighan's page now points to:

https://github.com/onetrueawk/awk

Looking through the history it includes some fixes from OpenBSD.

Chris