Description: this patch makes boinc correctly close the open files.
 This patch allows also MFILE to correctly deallocate its pointers by calling close().
Index: boinc/lib/crypt_prog.cpp
===================================================================
--- boinc.orig/lib/crypt_prog.cpp
+++ boinc/lib/crypt_prog.cpp
@@ -117,6 +117,7 @@ unsigned int random_int() {
     }
     if (1 != fread(&n, sizeof(n), 1, f)) {
         print_error("couldn't read from /dev/random\n");
+        fclose(f);
         return 2;
     }
     fclose(f);
@@ -156,6 +157,7 @@ int genkey(int n, const std::string& private_keyfile,
     FILE* fpub = fopen(public_keyfile.c_str(), "w");
     if (!fpub) {
         print_error("fopen");
+        fclose(fpriv);
         return 2;
     }
     print_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
@@ -176,6 +178,7 @@ int sign(const std::string& file, const std::string& private_keyfile) {
     int retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
     if (retval) {
         print_error("scan_key_hex");
+        fclose(fpriv);
         return 2;
     }
     const int data_len = 256;
@@ -186,9 +189,11 @@ int sign(const std::string& file, const std::string& private_keyfile) {
     retval = sign_file(file.c_str(), private_key, signature);
     if (retval) {
         print_error("sign_file");
+        fclose(fpriv);
         return 2;
     }
     print_hex_data(stdout, signature);
+    fclose(fpriv);
     return 0;
 }
 
@@ -202,6 +207,7 @@ int sign_string(const std::string& str, const std::string& private_keyfile) {
     int retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
     if (retval) {
         print_error("scan_key_hex");
+        fclose(fpriv);
         return 2;
     }
     char cbuf[512];
@@ -211,9 +217,11 @@ int sign_string(const std::string& str, const std::string& private_keyfile) {
         private_key);
     if (retval) {
         print_error("generate_signature");
+        fclose(fpriv);
         return 2;
     }
     std::cout << cbuf;
+    fclose(fpriv);
     return 0;
 }
 
@@ -228,11 +236,13 @@ int verify(const std::string& file, const std::string& signature_file,
     int retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
     if (retval) {
         print_error("read_public_key");
+        fclose(fpub);
         return 2;
     }
     FILE* f = fopen(signature_file.c_str(), "r");
     if (!f) {
         print_error("fopen");
+        fclose(fpub);
         return 2;
     }
     const int signature_len = 256;
@@ -243,6 +253,8 @@ int verify(const std::string& file, const std::string& signature_file,
     retval = scan_hex_data(f, signature);
     if (retval) {
         print_error("scan_hex_data");
+        fclose(fpub);
+        fclose(f);
         return 2;
     }
 
@@ -251,6 +263,8 @@ int verify(const std::string& file, const std::string& signature_file,
     retval = md5_file(file.c_str(), md5_buf, size);
     if (retval) {
         print_error("md5_file");
+        fclose(fpub);
+        fclose(f);
         return 2;
     }
     bool is_valid = false;
@@ -259,13 +273,19 @@ int verify(const std::string& file, const std::string& signature_file,
     );
     if (retval) {
         print_error("check_file_signature");
+        fclose(fpub);
+        fclose(f);
         return 2;
     }
 
     if (!is_valid) {
         std::cout << "signature is invalid" << std::endl;
+        fclose(fpub);
+        fclose(f);
         return 1;
     }
+    fclose(fpub);
+    fclose(f);
     std::cout << "signature is valid" << std::endl;
     return 0;
 }
@@ -281,11 +301,13 @@ int verify_string(const std::string& str, const std::string& signature_file,
     int retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
     if (retval) {
         print_error("read_public_key");
+        fclose(fpub);
         return 2;
     }
     FILE* f = fopen(signature_file.c_str(), "r");
     if (!f) {
         print_error("fopen");
+        fclose(fpub);
         return 2;
     }
     const int signature_len = 512;
@@ -298,13 +320,19 @@ int verify_string(const std::string& str, const std::string& signature_file,
     retval = check_string_signature(str.c_str(), cbuf, public_key, is_valid);
     if (retval) {
         print_error("check_string_signature");
+        fclose(fpub);
+        fclose(f);
         return 2;
     }
     if (!is_valid) {
         std::cout << "signature is invalid" << std::endl;
+        fclose(fpub);
+        fclose(f);
         return 1;
     }
     std::cout << "signature is valid" << std::endl;
+    fclose(fpub);
+    fclose(f);
     return 0;
 }
 
@@ -319,11 +347,13 @@ int test_crypt(const std::string& private_keyfile,
     int retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
     if (retval) {
         print_error("scan_key_hex\n");
+        fclose(fpriv);
         return 2;
     }
     FILE* fpub = fopen(public_keyfile.c_str(), "r");
     if (!fpub) {
         print_error("fopen");
+        fclose(fpriv);
         return 2;
     }
 
@@ -331,6 +361,8 @@ int test_crypt(const std::string& private_keyfile,
     retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
     if (retval) {
         print_error("read_public_key");
+        fclose(fpriv);
+        fclose(fpub);
         return 2;
     }
     const std::string test_string("encryption test successful");
@@ -343,6 +375,8 @@ int test_crypt(const std::string& private_keyfile,
     retval = encrypt_private(private_key, in, out);
     if (retval) {
         print_error("encrypt_private");
+        fclose(fpriv);
+        fclose(fpub);
         return 2;
     }
     in = out;
@@ -350,9 +384,13 @@ int test_crypt(const std::string& private_keyfile,
     retval = decrypt_public(public_key, in, out);
     if (retval) {
         print_error("decrypt_public");
+        fclose(fpriv);
+        fclose(fpub);
         return 2;
     }
     std::cout << "out: " << out.data << std::endl;
+    fclose(fpriv);
+    fclose(fpub);
     return 0;
 }
 
@@ -498,7 +536,9 @@ int convkey_private_o2b(const std::string& input, const std::string& output) {
         print_error("fopen");
         return 2;
     }
-    return print_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
+    int ret = print_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
+    fclose(fpriv);
+    return ret;
 }
 
 int convkey_public_b2o(const std::string& input, const std::string& output) {
@@ -580,7 +620,9 @@ int convkey_public_o2b(const std::string& input, const std::string& output) {
         print_error("fopen");
         return 2;
     }
-    return print_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
+    int ret = print_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
+    fclose(fpub);
+    return ret;
 }
 
 int convkey(const std::string& conversion, const std::string& key_type,
@@ -624,18 +666,21 @@ int cert_verify(const std::string& file, const std::string& signature_file,
     int retval = scan_hex_data(f, signature);
     if (retval) {
         print_error("cannot scan_hex_data");
+        fclose(f);
         return 2;
     }
     char* certpath = check_validity(certificate_dir.c_str(), file.c_str(),
         signature.data, const_cast<char*>(ca_dir.c_str()));
     if (certpath == NULL) {
         print_error("signature cannot be verified.");
+        fclose(f);
         return 2;
     }
     else {
         std::cout << "signature verified using certificate" << certpath
             << std::endl;
     }
+    fclose(f);
     return 0;
 }
 
Index: boinc/lib/mfile.cpp
===================================================================
--- boinc.orig/lib/mfile.cpp
+++ boinc/lib/mfile.cpp
@@ -40,7 +40,7 @@
 }
 
 MFILE::~MFILE() {
-    if (buf) free(buf);
+    close();
 }
 
 int MFILE::open(const char* path, const char* mode) {
Index: boinc/lib/parse_test.cpp
===================================================================
--- boinc.orig/lib/parse_test.cpp
+++ boinc/lib/parse_test.cpp
@@ -57,6 +57,7 @@
         exit(1);
     }
     parse(f);
+    fclose(f);
 }
 
 /* try it with something like:
