Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ endif
build: $(STB_IMAGE_NIF_SO)
@ echo > /dev/null

$(STB_IMAGE_NIF_SO):
$(STB_IMAGE_NIF_SO): $(C_SRC)/stb_image_nif.c $(C_SRC)/nif_utils.h $(wildcard $(STB_INCLUDE_DIR)/*.h)
@ mkdir -p $(PRIV_DIR)
$(CC) $(CPPFLAGS) $(C_SRC)/stb_image_nif.c -o $(STB_IMAGE_NIF_SO)
2 changes: 1 addition & 1 deletion Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CPPFLAGS = /O2 /EHsc /I"$(ERTS_INCLUDE_DIR)" /I"$(STB_INCLUDE_DIR)"

build: $(STB_IMAGE_NIF_SO)

$(STB_IMAGE_NIF_SO):
$(STB_IMAGE_NIF_SO): $(C_SRC)/stb_image_nif.c $(C_SRC)/nif_utils.h $(STB_INCLUDE_DIR)/stb_image.h $(STB_INCLUDE_DIR)/stb_image_write.h $(STB_INCLUDE_DIR)/stb_image_resize2.h
@ if not exist "$(PRIV_DIR)" mkdir "$(PRIV_DIR)"
$(CC) $(CPPFLAGS) /LD /MD /Fe$@ $(C_SRC)/stb_image_nif.c

Expand Down
9 changes: 7 additions & 2 deletions c_src/stb_image_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stb_image_resize2.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>

#define MAX_EXTNAME_LENGTH 4

Expand Down Expand Up @@ -56,14 +57,16 @@ static ERL_NIF_TERM read_file(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[

ERL_NIF_TERM ret;

if (!enif_inspect_binary(env, argv[0], &path)) {
if (!enif_inspect_binary(env, argv[0], &path) || path.size == SIZE_MAX ||
(path.size > 0 && memchr(path.data, '\0', path.size) != NULL)) {
return error(env, "invalid path");
}
if(!enif_get_int(env, argv[1], &desired_channels)) {
return error(env, "invalid channels");
}

c_path = enif_alloc(path.size + 1);
if (c_path == NULL) return error(env, "out of memory");
memcpy(c_path, path.data, path.size);
c_path[path.size] = '\0';

Expand Down Expand Up @@ -199,7 +202,8 @@ static ERL_NIF_TERM write_file(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv
ErlNifBinary result;
int w, h, comp;

if (!enif_inspect_binary(env, argv[0], &path)) {
if (!enif_inspect_binary(env, argv[0], &path) || path.size == SIZE_MAX ||
(path.size > 0 && memchr(path.data, '\0', path.size) != NULL)) {
return error(env, "invalid path");
}
if (!enif_get_atom(env, argv[1], format, sizeof(format), ERL_NIF_LATIN1)) {
Expand All @@ -219,6 +223,7 @@ static ERL_NIF_TERM write_file(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv
}

c_path = enif_alloc(path.size + 1);
if (c_path == NULL) return error(env, "out of memory");
memcpy(c_path, path.data, path.size);
c_path[path.size] = '\0';

Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule StbImage.MixProject do
use Mix.Project

@app :stb_image
@version "1.0.0"
@version "1.0.1-dev"
@github_url "https://github.com/elixir-nx/stb_image"

def project do
Expand All @@ -16,6 +16,7 @@ defmodule StbImage.MixProject do
docs: docs(),
package: package(),
compilers: [:elixir_make] ++ Mix.compilers(),
make_force_build: Mix.env() == :test,
make_precompiler: {:nif, CCPrecompiler},
make_precompiler_url: "#{@github_url}/releases/download/v#{@version}/@{artefact_filename}",
make_precompiler_filename: "stb_image_nif",
Expand Down
15 changes: 15 additions & 0 deletions test/stb_image_path_validation_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule StbImagePathValidationTest do
use ExUnit.Case, async: true

alias StbImage.Nif

@tag :tmp_dir
test "paths containing null bytes are rejected without truncation", %{tmp_dir: tmp_dir} do
source = Path.join(__DIR__, "test.png")
destination = Path.join(tmp_dir, "image.png")

assert {:error, _} = Nif.read_file(source <> <<0>> <> "suffix", 0)
assert {:error, _} = Nif.write_file(destination <> <<0>> <> "suffix", :png, <<0>>, 1, 1, 1)
refute File.exists?(destination)
end
end
Loading