]> mj.ucw.cz Git - subauth.git/commitdiff
Server: Fixed handling of exceptions during packet constructions
authorMartin Mares <mj@ucw.cz>
Wed, 6 Sep 2017 20:49:27 +0000 (22:49 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 6 Sep 2017 20:49:27 +0000 (22:49 +0200)
We fell into two LibUCW traps at once :)

First, fastbuf operations generate proper exceptions only when
the fastbuf is tied to a resource pool of the current transaction
by fb_tie(). Otherwise, they call plain die().

Second, fb-buf did not support bclose(), so it could not have been
tied.

LibUCW 6.5.5 fixes the second problem, so let us tie the fb-buf
properly.

server/subauthd.c

index df6d5e8ebf64005e6dc1b33c1d1874c17e9289cf..bdfe295930a57373f8df8d9452166bbd51c61476 100644 (file)
@@ -58,22 +58,26 @@ static void socket_timeout_handler(struct main_timer *tm)
 
 static void try_send_reply(struct client *c)
 {
-  struct fastbuf fb;
-  fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
+  int len;
 
   TRANS_TRY
     {
+      struct fastbuf fb;
+      fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
+      fb_tie(&fb);
       json_write(c->json, &fb, c->reply);
+      len = fbbuf_count_written(&fb);
     }
   TRANS_CATCH(x)
     {
       msg(L_ERROR, "Unable to construct reply, it is probably too long");
-      fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
-      bputs(&fb, "{ \"error\": \"Reply too long\" }\n");
+      struct fastbuf fb2;
+      fbbuf_init_write(&fb2, packet_buffer, MAX_PACKET_SIZE);
+      bputs(&fb2, "{ \"error\": \"Reply too long\" }\n");
+      len = fbbuf_count_written(&fb2);
     }
   TRANS_END;
 
-  int len = fbbuf_count_written(&fb);
   DBG("Sending reply of %d bytes", len);
   if (send(c->socket.fd, packet_buffer, len, 0) < 0)
     {