]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
qrcode-util: avoid memleak in error path
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 28 Oct 2024 12:45:40 +0000 (13:45 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 13 Nov 2024 19:48:10 +0000 (19:48 +0000)
(cherry picked from commit 439306da8b3962f683f5359c461d1669c070f377)

src/shared/qrcode-util.c

index 45b7920b48e07ed69412945bbedd900e4c284623..3c77af0ac7cf6d6a44575d5db1493366be2eb56b 100644 (file)
@@ -172,6 +172,8 @@ static void write_qrcode(FILE *output, QRcode *qr, unsigned int row, unsigned in
         fflush(output);
 }
 
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(QRcode*, sym_QRcode_free, NULL);
+
 int print_qrcode_full(
                 FILE *out,
                 const char *header,
@@ -182,7 +184,6 @@ int print_qrcode_full(
                 unsigned tty_height,
                 bool check_tty) {
 
-        QRcode* qr;
         int r;
 
         /* If this is not a UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
@@ -196,7 +197,8 @@ int print_qrcode_full(
         if (r < 0)
                 return r;
 
-        qr = sym_QRcode_encodeString(string, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
+        _cleanup_(sym_QRcode_freep) QRcode *qr =
+                sym_QRcode_encodeString(string, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
         if (!qr)
                 return log_oom_debug();
 
@@ -230,7 +232,6 @@ int print_qrcode_full(
         write_qrcode(out, qr, row, column);
         fputc('\n', out);
 
-        sym_QRcode_free(qr);
         return 0;
 }
 #endif