[master,kirkstone,1/5] lib/oe/cve_check: refactor update_symlinks with safer version

Message ID 20220628133713.3390786-1-davide.gardenal@huawei.com
State New, archived
Headers show
Series [master,kirkstone,1/5] lib/oe/cve_check: refactor update_symlinks with safer version | expand

Commit Message

Davide Gardenal June 28, 2022, 1:37 p.m. UTC
Now update_symlinks has more checks to prevent unwanted exception.
It returns False if the link is not created/updated, True otherwise.

Signed-off-by: Davide Gardenal <davide.gardenal@huawei.com>
---
 meta/lib/oe/cve_check.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

Patch

diff --git a/meta/lib/oe/cve_check.py b/meta/lib/oe/cve_check.py
index aa06497727..688693520e 100644
--- a/meta/lib/oe/cve_check.py
+++ b/meta/lib/oe/cve_check.py
@@ -169,7 +169,17 @@  def update_symlinks(target_path, link_path):
     Update a symbolic link link_path to point to target_path.
     Remove the link and recreate it if exist and is different.
     """
-    if link_path != target_path and os.path.exists(target_path):
-        if os.path.exists(os.path.realpath(link_path)):
-            os.remove(link_path)
-        os.symlink(os.path.basename(target_path), link_path)
+    import os
+
+    if target_path == link_path or \
+        target_path is None or \
+        link_path is None or \
+        not os.path.exists(target_path):
+
+        return False
+
+    if os.path.lexists(link_path):
+        os.remove(link_path)
+
+    os.symlink(target_path, link_path)
+    return True