Skip to content

Commit eae85a8

Browse files
committed
clearenv polyfill logic change to use secure functions
1 parent 5d458e8 commit eae85a8

2 files changed

Lines changed: 24 additions & 27 deletions

File tree

osfixes.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,23 @@
2121
#define TRUE 1
2222
#endif // !FALSE
2323

24-
int clearenv(void)
25-
{
26-
char* envp, * s;
27-
char name[MAX_LONG_PATH];
2824

25+
int clearenv(void) {
26+
char* envp, * s;
27+
char name[_MAX_ENV];
28+
int name_len;
2929

3030
while (environ && (envp = *environ)) {
3131
if ((s = strchr(envp, '=')) != NULL) {
32-
strncpy(name, envp, s - envp + 1);
33-
strncpy_s(name, sizeof(name), envp, s - envp+1);
34-
name[s - envp + 1+1] = 0;
32+
name_len = s - envp;
33+
strncpy_s(name, sizeof(name), envp, name_len);
34+
name[name_len + 1] = 0;
3535

36-
if (_putenv(name) == -1) {
36+
if (_putenv_s(name, "") == -1)
3737
return -1;
38-
}
3938
}
40-
else {
39+
else
4140
return -1;
42-
}
4341
}
4442
return 0;
4543
}
@@ -71,7 +69,7 @@ CONSTRUCTOR(wlb_at_startup) {
7169
#ifdef WLB_ALIGNED_ALLOC
7270
//memalign replacement note must use wlb_aligned_free to free
7371
void *wlb_aligned_alloc(size_t size, size_t alignment) {
74-
#ifndef _MSC_VER
72+
#ifndef _MSC_VER
7573
#ifdef HAVE_POSIX_MEMALIGN
7674
void *ptr=0;
7775
int result = posix_memalign(&ptr, alignment, size);

patches/repo_coreutils.patch

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ index 0e5d860a9..bcc0b3629 100644
368368

369369
/* This function checks whether any of the directories in the cycle that
370370
diff --git a/src/env.c b/src/env.c
371-
index 17254c303..8a1f9b615 100644
371+
index 17254c303..178debc8b 100644
372372
--- a/src/env.c
373373
+++ b/src/env.c
374374
@@ -53,7 +53,9 @@ enum SIGNAL_MODE {
@@ -377,7 +377,7 @@ index 17254c303..8a1f9b615 100644
377377
DEFAULT_NOERR, /* Ditto, but ignore sigaction(2) errors. */
378378
+#ifndef _WIN32
379379
IGNORE, /* Set to ignore (SIG_IGN). */
380-
+#endif
380+
+#endif
381381
IGNORE_NOERR /* Ditto, but ignore sigaction(2) errors. */
382382
};
383383
static enum SIGNAL_MODE *signals;
@@ -393,42 +393,41 @@ index 17254c303..8a1f9b615 100644
393393
+ if (sig2str (i, signame) == 0 && set_default)
394394
+ signals[i] = DEFAULT_NOERR;
395395
+ }
396-
+#endif
396+
+#endif
397397
return;
398398
}
399399

400-
@@ -755,7 +765,29 @@ initialize_signals (void)
400+
@@ -755,7 +765,28 @@ initialize_signals (void)
401401

402402
return;
403403
}
404404
-
405405
+#ifdef _WIN32
406-
+int clearenv(void)
407-
+{
406+
+#include <../ucrt/stdio.h>
407+
+static int clearenv(void) {
408408
+ char* envp, * s;
409-
+ char name[MAX_LONG_PATH];
409+
+ char name[_MAX_ENV];
410+
+ int name_len;
410411
+
411412
+ while (environ && (envp = *environ)) {
412413
+ if ((s = strchr(envp, '=')) != NULL) {
413-
+ strncpy(name, envp, s - envp + 1);
414-
+ strncpy_s(name, sizeof(name), envp, s - envp+1);
415-
+ name[s - envp + 1+1] = 0;
414+
+ name_len = s - envp;
415+
+ strncpy_s(name, sizeof(name), envp, name_len);
416+
+ name[name_len + 1] = 0;
416417
+
417-
+ if (_putenv(name) == -1) {
418+
+ if (_putenv_s(name, "") == -1)
418419
+ return -1;
419-
+ }
420420
+ }
421-
+ else {
421+
+ else
422422
+ return -1;
423-
+ }
424423
+ }
425424
+ return 0;
426425
+}
427426
+#endif
428427
int
429428
main (int argc, char **argv)
430429
{
431-
@@ -839,8 +871,12 @@ main (int argc, char **argv)
430+
@@ -839,8 +870,12 @@ main (int argc, char **argv)
432431
if (ignore_environment)
433432
{
434433
devmsg ("cleaning environ\n");

0 commit comments

Comments
 (0)