File 0795-stdlib-Fix-win32reg-delete_key.patch of Package erlang

From 6bbcfd7505f41545a8acf3680a60b75da582ca7c Mon Sep 17 00:00:00 2001
From: Regan Heath <reganmheath@gmail.com>
Date: Fri, 9 Jul 2021 01:27:00 +0100
Subject: [PATCH 1/2] stdlib: Fix win32reg:delete_key

The function win32reg:delete_key() did not work as documented.
It did not delete the current registry key and instead always
returned {error,einval} instead of ok.
---
 erts/emulator/drivers/win32/registry_drv.c |  8 +++++--
 lib/stdlib/src/win32reg.erl                | 20 ++++++++++++++---
 lib/stdlib/test/win32reg_SUITE.erl         | 25 ++++++++++++++++++++--
 3 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/erts/emulator/drivers/win32/registry_drv.c b/erts/emulator/drivers/win32/registry_drv.c
index 7d71ef7f53..a2fc9cffd1 100644
--- a/erts/emulator/drivers/win32/registry_drv.c
+++ b/erts/emulator/drivers/win32/registry_drv.c
@@ -318,8 +318,12 @@ reg_from_erlang(ErlDrvData clientData, char* buf, ErlDrvSizeT count)
 	}
 	break;
     case CMD_DELETE_KEY:
-	result = RegDeleteKey(rp->hkey, NULL);
-	reply(rp, result);
+	{
+	    char* key = buf;
+
+	    result = RegDeleteKey(rp->hkey, key);
+	    reply(rp, result);
+	}
 	break;
     case CMD_DELETE_VALUE:
 	result = RegDeleteValue(rp->hkey, buf);
diff --git a/lib/stdlib/src/win32reg.erl b/lib/stdlib/src/win32reg.erl
index 8e82a79cbf..e8e16980cb 100644
--- a/lib/stdlib/src/win32reg.erl
+++ b/lib/stdlib/src/win32reg.erl
@@ -143,9 +143,20 @@ sub_keys({win32reg, Reg}) when is_port(Reg) ->
       ReturnValue :: 'ok' | {'error', ErrorId :: atom()}.
 
 delete_key({win32reg, Reg}) when is_port(Reg) ->
-    Cmd = [?cmd_delete_key],
+    Cmd = [?cmd_get_current],
     Reg ! {self(), {command, Cmd}},
-    get_result(Reg).
+    case get_result(Reg) of
+	{state, _Hkey, []} ->
+	    {error, eperm};
+	{state, _Hkey, Rest} ->
+	    [Name|_] = split_key(Rest),
+	    ok = change_key(Reg, ?cmd_open_key, ".."),
+	    Cmd2 = [?cmd_delete_key],
+	    Reg ! {self(), {command, [Cmd2, Name, 0]}},
+	    get_result(Reg);
+	{error, Erorr} ->
+	    {error, Erorr}
+    end.
 
 -spec set_value(RegHandle, Name, Value) -> ReturnValue when
       RegHandle :: reg_handle(),
@@ -335,11 +346,14 @@ parse_relative(Path, Reg) ->
     Original = split_key(Name),
     Relative = lists:reverse(split_key(Path)),
     case parse_relative1(Relative, Original) of
+	{error,Error} ->
+	    {error,Error};
 	NewPath ->
 	    {ok, RootHandle, NewPath}
-    %% XXX Error handling.
     end.
 
+parse_relative1([".."|_], []) ->
+    {error,enoent};
 parse_relative1([".."|T1], [_|T2]) ->
     parse_relative1(T1, T2);
 parse_relative1([Comp|Rest], Result) ->
diff --git a/lib/stdlib/test/win32reg_SUITE.erl b/lib/stdlib/test/win32reg_SUITE.erl
index f7d3d8da97..14401128c1 100644
--- a/lib/stdlib/test/win32reg_SUITE.erl
+++ b/lib/stdlib/test/win32reg_SUITE.erl
@@ -20,7 +20,8 @@
 -module(win32reg_SUITE).
 
 -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
-	 init_per_group/2,end_per_group/2,long/1,evil_write/1]).
+	 init_per_group/2,end_per_group/2,long/1,evil_write/1,
+         delete_key/1, up_and_away/1]).
 
 -include_lib("test_server/include/test_server.hrl").
 
@@ -27,7 +28,10 @@ suite() ->
 suite() -> [{ct_hooks,[ts_install_cth]}].
 
 all() -> 
-    [long, evil_write].
+    [long,
+     evil_write,
+     delete_key,
+     up_and_away].
 
 groups() -> 
     [].
@@ -105,3 +109,20 @@ evil_write_1(Reg, [_|[_|_]=Key]=Key0) ->
     ?line ok = win32reg:delete_value(Reg, Key0),
     evil_write_1(Reg, Key);
 evil_write_1(_, [_]) -> ok.
+
+delete_key(Config) when is_list(Config) ->
+    Key = "Software\\Ericsson\\Erlang\\new-test-key",
+    {ok,Reg} = win32reg:open([read,write]),
+    ok = win32reg:change_key(Reg, "\\hkcu"),
+    ok = win32reg:change_key_create(Reg, Key),
+    {ok, []} = win32reg:sub_keys(Reg),
+    ok = win32reg:delete_key(Reg),
+    ok = win32reg:close(Reg),
+    ok.
+
+up_and_away(Config) when is_list(Config) ->
+    {ok,Reg} = win32reg:open([read]),
+    {ok, "\\hkey_classes_root"} = win32reg:current_key(Reg),
+    {error,enoent} = win32reg:change_key(Reg, ".."),
+    ok = win32reg:close(Reg),
+    ok.
-- 
2.31.1

openSUSE Build Service is sponsored by