Ver código fonte

Fix CodeQL findings (fc-match without a shell, PPM dump mode, float widening) and filter third-party results from the scan

UwU 4 semanas atrás
pai
commit
fecf647ddd

+ 33 - 3
.github/workflows/codeql.yml

@@ -36,9 +36,6 @@ jobs:
           languages: c-cpp
           build-mode: manual
           queries: security-extended
-          config: |
-            paths-ignore:
-              - src/External
 
       - name: Build
         run: |
@@ -49,3 +46,36 @@ jobs:
         uses: github/codeql-action/analyze@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4
         with:
           category: /language:c-cpp
+          upload: never
+          output: sarif-results
+
+      # paths-ignore does not apply to a manual C++ build, so third-party results are dropped from the SARIF here
+      - name: Drop third-party results
+        run: |
+          python3 - <<'EOF'
+          import glob, json
+
+          skipped = ("src/External/", "build/")
+
+          for name in glob.glob("sarif-results/*.sarif"):
+              with open(name) as f:
+                  sarif = json.load(f)
+
+              for run in sarif["runs"]:
+                  run["results"] = [
+                      r for r in run.get("results", [])
+                      if not any(
+                          l["physicalLocation"]["artifactLocation"]["uri"].startswith(skipped)
+                          for l in r.get("locations", [])
+                      )
+                  ]
+
+              with open(name, "w") as f:
+                  json.dump(sarif, f)
+          EOF
+
+      - name: Upload results
+        uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4
+        with:
+          sarif_file: sarif-results
+          category: /language:c-cpp

+ 63 - 9
src/WallpaperEngine/Render/Fonts/SystemFonts.cpp

@@ -1,32 +1,87 @@
 #include "SystemFonts.h"
 
 #include <algorithm>
+#include <cerrno>
 #include <chrono>
 #include <cstdio>
 #include <cstdlib>
+#include <cstring>
+#include <fcntl.h>
 #include <fstream>
 #include <mutex>
+#include <spawn.h>
 #include <sstream>
 #include <strings.h>
+#include <sys/wait.h>
+#include <unistd.h>
 #include <unordered_map>
 
+extern char** environ;
+
 namespace WallpaperEngine::Render::Fonts {
 namespace {
-    std::string run (const std::string& command) {
-	FILE* pipe = popen (command.c_str (), "r");
+    std::string run (const std::string& config, const std::string& pattern, const std::string& format) {
+	std::vector<std::string> environment;
+
+	for (char** entry = environ; *entry != nullptr; entry++) {
+	    if (!config.empty () && strncmp (*entry, "FONTCONFIG_FILE=", 16) == 0) {
+		continue;
+	    }
+
+	    environment.emplace_back (*entry);
+	}
+
+	if (!config.empty ()) {
+	    environment.push_back ("FONTCONFIG_FILE=" + config);
+	}
+
+	std::vector<char*> envp;
+
+	for (std::string& entry : environment) {
+	    envp.push_back (entry.data ());
+	}
 
-	if (pipe == nullptr) {
+	envp.push_back (nullptr);
+
+	std::string fcMatch = "fc-match";
+	std::string formatFlag = "-f";
+	std::string formatCopy = format;
+	std::string patternCopy = pattern;
+	char* argv[] = { fcMatch.data (), formatFlag.data (), formatCopy.data (), patternCopy.data (), nullptr };
+
+	int fds[2];
+
+	if (pipe2 (fds, O_CLOEXEC) != 0) {
 	    return {};
 	}
 
+	posix_spawn_file_actions_t actions;
+	posix_spawn_file_actions_init (&actions);
+	posix_spawn_file_actions_adddup2 (&actions, fds[1], STDOUT_FILENO);
+	posix_spawn_file_actions_addopen (&actions, STDERR_FILENO, "/dev/null", O_WRONLY, 0);
+
+	pid_t child = 0;
+	const int started = posix_spawnp (&child, "fc-match", &actions, nullptr, argv, envp.data ());
+
+	posix_spawn_file_actions_destroy (&actions);
+	close (fds[1]);
+
 	std::string output;
-	char buffer[512];
 
-	while (fgets (buffer, sizeof (buffer), pipe) != nullptr) {
-	    output += buffer;
+	if (started == 0) {
+	    char buffer[512];
+	    ssize_t count;
+
+	    while ((count = read (fds[0], buffer, sizeof (buffer))) > 0 || (count < 0 && errno == EINTR)) {
+		if (count > 0) {
+		    output.append (buffer, static_cast<size_t> (count));
+		}
+	    }
+
+	    waitpid (child, nullptr, 0);
 	}
 
-	pclose (pipe);
+	close (fds[0]);
 	return output;
     }
 
@@ -182,8 +237,7 @@ std::string fcMatch (const std::string& pattern, const std::string& format) {
     }
 
     const std::string config = fontconfigFile ();
-    const std::string prefix = config.empty () ? std::string () : "FONTCONFIG_FILE='" + config + "' ";
-    std::string answer = run (prefix + "fc-match -f '" + format + "' '" + pattern + "' 2>/dev/null");
+    std::string answer = run (config, pattern, format);
 
     const std::scoped_lock lock (mutex);
 

+ 5 - 1
src/WallpaperEngine/Render/RenderContext.cpp

@@ -17,7 +17,9 @@
 #include <cmath>
 #include <cstdio>
 #include <cstdlib>
+#include <fcntl.h>
 #include <numeric>
+#include <unistd.h>
 #include <unordered_map>
 #include <vector>
 
@@ -494,7 +496,9 @@ void main () {
 	glPixelStorei (GL_PACK_ALIGNMENT, 1);
 	glReadPixels (rect.x, rect.y, rect.z, rect.w, GL_RGB, GL_UNSIGNED_BYTE, pixels.data ());
 
-	if (std::FILE* file = std::fopen (path.c_str (), "wb"); file != nullptr) {
+	const int descriptor = open (path.c_str (), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+
+	if (std::FILE* file = descriptor >= 0 ? fdopen (descriptor, "wb") : nullptr; file != nullptr) {
 	    std::fprintf (file, "P6\n%d %d\n255\n", rect.z, rect.w);
 	    std::fwrite (pixels.data (), 1, pixels.size (), file);
 	    std::fclose (file);

+ 1 - 1
src/WallpaperEngine/Scripting/Adapters/ScriptableObjectAdapter.cpp

@@ -2450,7 +2450,7 @@ v8::Local<v8::Value> ScriptableObjectAdapter::material (ImageEffect& effect, siz
 		      case 4:
 			  return engine.getAdapters ().vec4->create (vector);
 		      default:
-			  return v8::Number::New (isolate, value->getFloat () * factor);
+			  return v8::Number::New (isolate, static_cast<double> (value->getFloat ()) * factor);
 		  }
 	      },
 	      .set =