chiark / gitweb /
regress: hfuzzraw: Fix a wrong pointer passed to fread
[adns.git] / regress / hfuzzraw.c.m4
index c575ddb208b0269f01c06fddc0689199051f9889..f4892799d6a5971dcff23b3f4ef8ab3b6eca21c5 100644 (file)
@@ -64,6 +64,11 @@ static void Pcheckinput(void) {
 }
 
 void Tensurerecordfile(void) {
+  static int done;
+
+  if (done) return;
+  done++;
+
   int fd;
 
   fd = Ttestinputfd();
@@ -78,22 +83,25 @@ void Tensurerecordfile(void) {
 }
 
 static void P_read(void *p, size_t sz) {
-  ssize_t got = fread(&p,1,sz,Tinputfile);
+  ssize_t got = fread(p,1,sz,Tinputfile);
   Pcheckinput();
   assert(got==sz);
 }
 
 #define P_READ(x) (P_read(&(x), sizeof((x))))
 
-static void P_updatetime(void) {
-m4_dnl xxx
-}
-
 static unsigned P_fdf(int fd) {
   assert(fd>=0 && fd<fdtab.used);
   return fdtab.buf[fd];
 }
 
+void T_gettimeofday_hook(void) {
+  struct timeval delta, sum;
+  P_READ(delta);
+  timeradd(&delta, &currenttime, &sum);
+  currenttime= sum;
+}
+
 static void Paddr(struct sockaddr *addr, int *lenr) {
   int l, r;
   uint16_t port;
@@ -167,7 +175,6 @@ int H$1(hm_args_massage($3,void)) {
  $3
 
  Tensurerecordfile();
- P_updatetime();
 
  m4_define(`hm_rv_succfail',`
   P_READ(r);
@@ -214,15 +221,17 @@ int H$1(hm_args_massage($3,void)) {
  m4_define(`hm_rv_fd',`
   hm_rv_succfail
   if (!r) {
-    for (;;) {
-      assert(r < 1000);
-      if (r >= fdtab.used)
-        if (!adns__vbuf_append(&fdtab,"\0",1)) Tnomem();
-      assert(r < fdtab.used);
-      if (!(fdtab.buf[r] & FDF_OPEN)) break;
-      r++;
+    int newfd;
+    P_READ(newfd);
+    if (newfd<0 || newfd>1000) Pformat("new fd out of range");
+    adns__vbuf_ensure(&fdtab, newfd+1);
+    if (fdtab.used <= newfd) {
+      memset(fdtab.buf+fdtab.used, 0, newfd+1-fdtab.used);
+      fdtab.used= newfd+1;
     }
-    fdtab.buf[r] |= FDF_OPEN;
+    if (fdtab.buf[newfd]) Pformat("new fd already in use");
+    fdtab.buf[newfd] |= FDF_OPEN;
+    r= newfd;
  }
  ')
  $2
@@ -237,7 +246,6 @@ int H$1(hm_args_massage($3,void)) {
  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
  $3
 
- P_updatetime();
  return r;
 }
 ')
@@ -246,3 +254,10 @@ m4_define(`hm_specsyscall', `')
 
 m4_include(`hsyscalls.i4')
 
+int Hclose(int fd) {
+  int r;
+  P_fdf(fd);
+  fdtab.buf[fd]= 0;
+  hm_rv_succfail
+  return r;
+}