commit b5c9579e6c5505125ac9ce171891e126c1c728e0 from: Kyle Ackerman via: Thomas Adam date: Thu May 15 16:08:55 2025 UTC Plug memory leaks in some libexecs This occurred when these particular libexecs get an imsg of type GOT_IMSG_STOP. They attempt to exit the main loop and leak the last imsg they received. ok stsp@ op@ commit - 6b89d48589d6584c63c8b9dd8b0abde8501bcc92 commit + b5c9579e6c5505125ac9ce171891e126c1c728e0 blob - 422e04b7301dc1eabacadbf80babcfbf4a4a780c blob + ed4a42c8e7fbdbf1ea8ba1623c3b4086ea2975b4 --- libexec/got-read-blob/got-read-blob.c +++ libexec/got-read-blob/got-read-blob.c @@ -89,7 +89,7 @@ main(int argc, char *argv[]) for (;;) { struct imsg imsg, imsg_outfd; FILE *f = NULL; - int fd = -1, outfd = -1; + int fd = -1, outfd = -1, finished = 0; size_t size; struct got_object *obj = NULL; uint8_t *buf = NULL; @@ -113,9 +113,12 @@ main(int argc, char *argv[]) break; } - if (imsg.hdr.type == GOT_IMSG_STOP) - break; + if (imsg.hdr.type == GOT_IMSG_STOP) { + finished = 1; + goto done; + } + if (imsg.hdr.type != GOT_IMSG_BLOB_REQUEST) { err = got_error(GOT_ERR_PRIVSEP_MSG); goto done; @@ -213,9 +216,12 @@ done: imsg_free(&imsg); imsg_free(&imsg_outfd); - if (obj) + if (obj) { got_object_close(obj); - if (err) + obj = NULL; + } + + if (err || finished) break; } blob - 4ac05983cd59f84ead60fa06a07e955c296526be blob + 23b2abd2788e35db9dd24a8099d4a6d5649c8245 --- libexec/got-read-gitconfig/got-read-gitconfig.c +++ libexec/got-read-gitconfig/got-read-gitconfig.c @@ -376,7 +376,7 @@ main(int argc, char *argv[]) for (;;) { struct imsg imsg; - int fd = -1; + int fd = -1, finished = 0; memset(&imsg, 0, sizeof(imsg)); @@ -392,8 +392,10 @@ main(int argc, char *argv[]) break; } - if (imsg.hdr.type == GOT_IMSG_STOP) - break; + if (imsg.hdr.type == GOT_IMSG_STOP) { + finished = 1; + goto done; + } switch (imsg.hdr.type) { case GOT_IMSG_GITCONFIG_PARSE_REQUEST: @@ -438,13 +440,14 @@ main(int argc, char *argv[]) break; } + done: if (fd != -1) { if (close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); } imsg_free(&imsg); - if (err) + if (err || finished) break; } blob - 023e1e44753cb9815d503bb8fa0ce3cd6cfc8dbb blob + 5a37a25517b9cd8bab47170ae805b3ac8d244c4d --- libexec/got-read-gotconfig/got-read-gotconfig.c +++ libexec/got-read-gotconfig/got-read-gotconfig.c @@ -523,7 +523,7 @@ main(int argc, char *argv[]) for (;;) { struct imsg imsg; - int fd = -1; + int fd = -1, finished = 0; memset(&imsg, 0, sizeof(imsg)); @@ -539,8 +539,10 @@ main(int argc, char *argv[]) break; } - if (imsg.hdr.type == GOT_IMSG_STOP) - break; + if (imsg.hdr.type == GOT_IMSG_STOP) { + finished = 1; + goto done; + } switch (imsg.hdr.type) { case GOT_IMSG_GOTCONFIG_PARSE_REQUEST: @@ -608,14 +610,14 @@ main(int argc, char *argv[]) err = got_error(GOT_ERR_PRIVSEP_MSG); break; } - + done: if (fd != -1) { if (close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); } imsg_free(&imsg); - if (err) + if (err || finished) break; } blob - c9b8f3a854b61080b46effafa1086a6a3ad9f1af blob + 9bd7dc6de046f0f489c6c1a59c01fa89884c6a28 --- libexec/got-read-object/got-read-object.c +++ libexec/got-read-object/got-read-object.c @@ -127,7 +127,7 @@ main(int argc, char *argv[]) #endif for (;;) { - int fd = -1, outfd = -1; + int fd = -1, outfd = -1, finished = 0; if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); @@ -141,8 +141,10 @@ main(int argc, char *argv[]) break; } - if (imsg.hdr.type == GOT_IMSG_STOP) - break; + if (imsg.hdr.type == GOT_IMSG_STOP) { + finished = 1; + goto done; + } if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST && imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) { @@ -214,9 +216,11 @@ done: if (fd != -1 && close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); imsg_free(&imsg); - if (obj) + if (obj) { got_object_close(obj); - if (err) + obj = NULL; + } + if (err || finished) break; }