Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 1 | From f5de3401b974ce103ffd93af8f9d43505a04aaf9 Mon Sep 17 00:00:00 2001 |
| 2 | From: Damian Kurek <starfire24680@gmail.com> |
| 3 | Date: Thu, 7 Jul 2022 03:39:16 +0000 |
| 4 | Subject: [PATCH] Fix NULL dereference when duplicating string argument |
| 5 | |
| 6 | poptGetArg can return NULL if there are no additional arguments, which |
| 7 | makes strdup dereference NULL on strlen |
| 8 | |
| 9 | Upstream-Status: Submitted [https://sourceforge.net/p/gptfdisk/code/merge-requests/28/] |
| 10 | |
| 11 | --- |
| 12 | gptcl.cc | 6 ++++-- |
| 13 | 1 file changed, 4 insertions(+), 2 deletions(-) |
| 14 | |
| 15 | diff --git a/gptcl.cc b/gptcl.cc |
| 16 | index 0d578eb..ab95239 100644 |
| 17 | --- a/gptcl.cc |
| 18 | +++ b/gptcl.cc |
| 19 | @@ -155,10 +155,11 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) { |
| 20 | } // while |
| 21 | |
| 22 | // Assume first non-option argument is the device filename.... |
| 23 | - device = strdup((char*) poptGetArg(poptCon)); |
| 24 | - poptResetContext(poptCon); |
| 25 | + device = (char*) poptGetArg(poptCon); |
| 26 | |
| 27 | if (device != NULL) { |
| 28 | + device = strdup(device); |
| 29 | + poptResetContext(poptCon); |
| 30 | JustLooking(); // reset as necessary |
| 31 | BeQuiet(); // Tell called functions to be less verbose & interactive |
| 32 | if (LoadPartitions((string) device)) { |
| 33 | @@ -498,6 +499,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) { |
| 34 | cerr << "Error encountered; not saving changes.\n"; |
| 35 | retval = 4; |
| 36 | } // if |
| 37 | + free(device); |
| 38 | } // if (device != NULL) |
| 39 | poptFreeContext(poptCon); |
| 40 | return retval; |
| 41 | |