Skip to content

Commit 25fe46a

Browse files
committed
Use gnulib module "setenv" instead of "putenv-gnu" (bug #67378).
* bootstrap.conf: Remove "putenv-gnu" from list of imported gnulib modules. Add "setenv". * liboctave/wrappers/putenv-wrapper.c, liboctave/wrappers/putenv-wrapper.h: Remove wrappers for gnulib function. * liboctave/wrappers/setenv-wrapper.c, liboctave/wrappers/setenv-wrapper.h: Add wrappers for gnulib function. * liboctave/wrappers/module.mk: Remove deleted files from build system. Add new files to build system. * liboctave/system/oct-sysdep.cc: Remove inclusion of deleted header. Include new header (octave::sys::putenv_wrapper): Add check of input parameter on Windows. Use wrapper to gnulib function setenv on any other platform (octave::sys::unsetenv_wrapper): Add comment.
1 parent e8d33d1 commit 25fe46a

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

bootstrap.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ gnulib_modules="
7979
opendir
8080
pipe-posix
8181
progname
82-
putenv-gnu
8382
raise
8483
readdir
8584
readline
8685
rewinddir
8786
rmdir
8887
select
88+
setenv
8989
setlocale
9090
sigaction
9191
signal-h

liboctave/system/oct-sysdep.cc

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "localcharset-wrapper.h"
3838
#include "oct-error.h"
3939
#include "oct-sysdep.h"
40-
#include "putenv-wrapper.h"
40+
#include "setenv-wrapper.h"
4141
#include "unistd-wrappers.h"
4242
#include "unsetenv-wrapper.h"
4343

@@ -647,16 +647,21 @@ ofstream (const std::string& filename, const std::ios::openmode mode)
647647
void
648648
putenv_wrapper (const std::string& name, const std::string& value)
649649
{
650-
std::string new_env = name + "=" + value;
651-
652-
// FIXME: The malloc leaks memory, but so would a call to setenv.
653650
// Short of extreme measures to track memory, altering the environment
654651
// always leaks memory, but the saving grace is that the leaks are small.
655652

653+
#if defined (OCTAVE_USE_WINDOWS_API)
654+
// FIXME: The malloc leaks memory, but so would a call to setenv.
655+
656656
// As far as I can see there's no way to distinguish between the
657657
// various errors; putenv doesn't have errno values.
658658

659-
#if defined (OCTAVE_USE_WINDOWS_API)
659+
if (name.find ('=') != std::string::npos)
660+
(*current_liboctave_error_handler)
661+
("putenv: name (%s) must not contain '='", name.c_str());
662+
663+
std::string new_env = name + "=" + value;
664+
660665
std::wstring new_wenv = u8_to_wstring (new_env);
661666

662667
int len = (new_wenv.length () + 1) * sizeof (wchar_t);
@@ -669,14 +674,12 @@ putenv_wrapper (const std::string& name, const std::string& value)
669674
(*current_liboctave_error_handler)
670675
("putenv (%s) failed", new_env.c_str());
671676
#else
672-
int len = new_env.length () + 1;
673-
674-
char *new_item = static_cast<char *> (std::malloc (len));
675-
676-
std::strcpy (new_item, new_env.c_str());
677-
678-
if (octave_putenv_wrapper (new_item) < 0)
679-
(*current_liboctave_error_handler) ("putenv (%s) failed", new_item);
677+
// FIXME: Using setenv leaks memory, but so would using malloc and putenv.
678+
679+
if (octave_setenv_wrapper (name.c_str (), value.c_str (), 1) < 0)
680+
(*current_liboctave_error_handler)
681+
("setenv (%s, %s) failed with error %d", name.c_str (), value.c_str (),
682+
errno);
680683
#endif
681684
}
682685

@@ -699,6 +702,7 @@ unsetenv_wrapper (const std::string& name)
699702
#if defined (OCTAVE_USE_WINDOWS_API)
700703
putenv_wrapper (name, "");
701704

705+
// Also remove variable from environment.
702706
std::wstring wname = u8_to_wstring (name);
703707
return (SetEnvironmentVariableW (wname.c_str (), nullptr) ? 0 : -1);
704708
#else

liboctave/wrappers/module.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ NOINSTALL_WRAPPERS_INC = \
2020
%reldir%/nanosleep-wrapper.h \
2121
%reldir%/nproc-wrapper.h \
2222
%reldir%/octave-popen2.h \
23-
%reldir%/putenv-wrapper.h \
2423
%reldir%/pwd-wrappers.h \
2524
%reldir%/select-wrappers.h \
2625
%reldir%/set-program-name-wrapper.h \
26+
%reldir%/setenv-wrapper.h \
2727
%reldir%/signal-wrappers.h \
2828
%reldir%/stat-wrappers.h \
2929
%reldir%/strcase-wrappers.h \
@@ -66,10 +66,10 @@ WRAPPERS_SRC = \
6666
%reldir%/nanosleep-wrapper.c \
6767
%reldir%/nproc-wrapper.c \
6868
%reldir%/octave-popen2.c \
69-
%reldir%/putenv-wrapper.c \
7069
%reldir%/pwd-wrappers.c \
7170
%reldir%/select-wrappers.c \
7271
%reldir%/set-program-name-wrapper.c \
72+
%reldir%/setenv-wrapper.c \
7373
%reldir%/signal-wrappers.c \
7474
%reldir%/stat-wrappers.c \
7575
%reldir%/strcase-wrappers.c \

liboctave/wrappers/putenv-wrapper.c renamed to liboctave/wrappers/setenv-wrapper.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
////////////////////////////////////////////////////////////////////////
22
//
3-
// Copyright (C) 2016-2025 The Octave Project Developers
3+
// Copyright (C) 2025 The Octave Project Developers
44
//
55
// See the file COPYRIGHT.md in the top-level directory of this
66
// distribution or <https://octave.org/copyright/>.
@@ -23,7 +23,7 @@
2323
//
2424
////////////////////////////////////////////////////////////////////////
2525

26-
// putenv may be provided by gnulib. We don't include gnulib headers
26+
// unsetenv may be provided by gnulib. We don't include gnulib headers
2727
// directly in Octave's C++ source files to avoid problems that may be
2828
// caused by the way that gnulib overrides standard library functions.
2929

@@ -33,10 +33,10 @@
3333

3434
#include <stdlib.h>
3535

36-
#include "putenv-wrapper.h"
36+
#include "setenv-wrapper.h"
3737

3838
int
39-
octave_putenv_wrapper (char *str)
39+
octave_setenv_wrapper (const char *envname, const char *envval, int overwrite)
4040
{
41-
return putenv (str);
41+
return setenv (envname, envval, overwrite);
4242
}

liboctave/wrappers/putenv-wrapper.h renamed to liboctave/wrappers/setenv-wrapper.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
////////////////////////////////////////////////////////////////////////
22
//
3-
// Copyright (C) 2016-2025 The Octave Project Developers
3+
// Copyright (C) 2025 The Octave Project Developers
44
//
55
// See the file COPYRIGHT.md in the top-level directory of this
66
// distribution or <https://octave.org/copyright/>.
@@ -23,14 +23,15 @@
2323
//
2424
////////////////////////////////////////////////////////////////////////
2525

26-
#if ! defined (octave_putenv_wrapper_h)
27-
#define octave_putenv_wrapper_h 1
26+
#if ! defined (octave_setenv_wrapper_h)
27+
#define octave_setenv_wrapper_h 1
2828

2929
#if defined (__cplusplus)
3030
extern "C" {
3131
#endif
3232

33-
extern OCTAVE_API int octave_putenv_wrapper (char *str);
33+
extern OCTAVE_API int
34+
octave_setenv_wrapper (const char *envname, const char *envval, int overwrite);
3435

3536
#if defined (__cplusplus)
3637
}

0 commit comments

Comments
 (0)