From 453929342f1d7091730bdb80fb23722a7fe98aaa Mon Sep 17 00:00:00 2001
From: Serge Lamikhov-Center <to_serge@users.sourceforge.net>
Date: Fri, 29 May 2020 05:26:43 -0700
Subject: [PATCH] Don't access a note entry in case namesz is less than 1

---
 .vscode/launch.json  | 25 +++++++++++++++++++++++--
 .vscode/tasks.json   | 18 +++++++++++++++++-
 elfio/elfio_note.hpp |  5 +++--
 3 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/.vscode/launch.json b/.vscode/launch.json
index 27fdcbf..df9d82d 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -5,7 +5,7 @@
     "version": "0.2.0",
     "configurations": [
         {
-            "name": "g++ build and debug active file",
+            "name": "Run ELFIO Tests",
             "type": "cppdbg",
             "request": "launch",
             "program": "${workspaceFolder}/ELFIOTest/ELFIOTest",
@@ -22,7 +22,28 @@
                     "ignoreFailures": true
                 }
             ],
-            "preLaunchTask": "g++ build",
+            "preLaunchTask": "ELFIO Test build",
+            "miDebuggerPath": "/usr/bin/gdb"
+        },
+        {
+            "name": "Run ELF Dump",
+            "type": "cppdbg",
+            "request": "launch",
+            "program": "${workspaceFolder}/examples/elfdump/elfdump",
+            "args": ["test"],
+            "stopAtEntry": false,
+            "cwd": "${workspaceFolder}",
+            "environment": [],
+            "externalConsole": false,
+            "MIMode": "gdb",
+            "setupCommands": [
+                {
+                    "description": "Enable pretty-printing for gdb",
+                    "text": "-enable-pretty-printing",
+                    "ignoreFailures": true
+                }
+            ],
+            "preLaunchTask": "ELF Dump Build",
             "miDebuggerPath": "/usr/bin/gdb"
         }
     ]
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index bb508cf..5ff6952 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -2,7 +2,7 @@
     "tasks": [
         {
             "type": "shell",
-            "label": "g++ build",
+            "label": "ELFIO Test build",
             "command": "make",
             "args": [
                 "INCLUDES=-I..",
@@ -15,6 +15,22 @@
                 "kind": "build",
                 "isDefault": true
             }
+        },
+        {
+            "type": "shell",
+            "label": "ELF Dump Build",
+            "command": "make",
+            "args": [
+                "INCLUDES=-I..",
+                "CXXFLAGS='-g -O0'"
+            ],
+            "options": {
+                "cwd": "${workspaceRoot}",
+            },
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            }
         }
     ],
     "version": "2.0.0"
diff --git a/elfio/elfio_note.hpp b/elfio/elfio_note.hpp
index 8619c73..fad615e 100644
--- a/elfio/elfio_note.hpp
+++ b/elfio/elfio_note.hpp
@@ -74,9 +74,10 @@ class note_section_accessor_template
         const endianess_convertor& convertor = elf_file.get_convertor();
         type = convertor( *(const Elf_Word*)( pData + 2*align ) );
         Elf_Word namesz = convertor( *(const Elf_Word*)( pData ) );
-        descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) );
+        descSize        = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) );
         Elf_Xword max_name_size = note_section->get_size() - note_start_positions[index];
-        if ( namesz            > max_name_size ||
+        if ( namesz            < 1             ||
+             namesz            > max_name_size ||
              namesz + descSize > max_name_size ) {
             return false;
         }
-- 
GitLab