
From f46625e444f2bbab110e217a34abd0898a242c11 Mon Sep 17 00:00:00 2001
From: chenziyang <chenziyang4@huawei.com>
Date: Sun, 5 Mar 2023 17:04:09 +0800
Subject: [PATCH 2/2] prohibit multiple -i options and do not allow -i specify non
 PT_LOAD segment

Signed-off-by: Chen Ziyang<chenziyang4@huawei.com>

---
 elf/hugepageedit.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/elf/hugepageedit.c b/elf/hugepageedit.c
index 0a44ece6..38b4db70 100644
--- a/elf/hugepageedit.c
+++ b/elf/hugepageedit.c
@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
   size_t length;
   int exit_status = -1;
   int i, opt, delete = 0, exec_only = 0, index_set = 0;
-  long index = -1;
+  long index = -1, index_count = 0;
   while ((opt = getopt(argc, argv, "dxi:")) != -1)
     {
       switch (opt)
@@ -91,6 +91,11 @@ int main(int argc, char *argv[])
             index_set = 1;
             if (index < 0)
                 return -1;
+            index_count++;
+            if (index_count > 1) {
+              fprintf(stderr, "too many -i options in command line\n");
+              return -1;
+            }
             break;
           default:
             print_usage();
@@ -150,6 +155,12 @@ int main(int argc, char *argv[])
                 continue;
               phdr[i].p_flags |= PF_HUGEPAGE;
             }
+        } else {
+          if (index_set && index == i) {
+            fprintf(stderr, "Index %ld in -i %s option is not a PT_LOAD segment\n",
+                    index, argv[optind]);
+            goto unmap;
+          }
         }
     }
   exit_status = 0;
--
2.33.0

