chiark / gitweb /
fix cht_get_urandom error handling
[chiark-tcl.git] / base / hook.c
index 82d01cfb6fd75d9dc0aa5ac46f021065964c7786..6e4b3a102e4abe26cf48cdb26faad2066dd2d439 100644 (file)
@@ -84,7 +84,7 @@ void cht_obj_updatestr_string(Tcl_Obj *o, const char *str) {
 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
   static FILE *urandom;
 
-  int r, esave;
+  int r;
 
   if (!urandom) {
     urandom= fopen(URANDOM,"rb");
@@ -93,15 +93,14 @@ int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
   r= fread(buffer,1,l,urandom);
   if (r==l) return 0;
 
-  esave= errno;
-  fclose(urandom); urandom=0;
-
   if (ferror(urandom)) {
-    return cht_posixerr(ip,errno,"read " URANDOM);
+    r = cht_posixerr(ip,errno,"read " URANDOM);
   } else {
     assert(feof(urandom));
-    return cht_staticerr(ip, URANDOM " gave eof!", 0);
+    r = cht_staticerr(ip, URANDOM " gave eof!", 0);
   }
+  fclose(urandom); urandom=0;
+  return r;
 }
 
 void cht_prepare__basic(Tcl_Interp *ip) {