libc: rewrite putenv and getenv.

These new implementations

- do several validity check on input parameters
- allow a bit larger variable names (127 bytes, aka sizeof(Proc.genbuf)-1)
- preserve nulls in the content (the original version used to replace
  '\0' with ' '). I can't see why they did, actually.
  See also http://marc.info/?l=9fans&m=148475801229908&w=2

Should also fix CID 155718
This commit is contained in:
Giacomo Tesio 2017-01-19 00:58:43 +01:00
parent d43be3861b
commit 65cdad4317
6 changed files with 118 additions and 58 deletions

View File

@ -8,6 +8,9 @@
"-std=gnu11"
],
"Install": "/arch/$ARCH/qa/lib/c",
"Post": [
"cp *.rc $JEHANNE/arch/$ARCH/qa/lib/c/"
],
"SourceFilesCmd": [
"asmscall.c",
"cleanname.c",

28
qa/lib/c/env.rc Executable file
View File

@ -0,0 +1,28 @@
#!/cmd/rc
# verify that environment variables can have names 127 byte long
# why 127? because it's the size of genbuf in the kernel's Proc structure
# minus the ending \0
abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345678=10
if ( ! ~ $abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345678 10 ) {
echo FAIL: cannot read an environment variable with a long name
exit FAIL
}
# verify that rc lists work
list=(How now brown cow)
string=$"list
if( ! ~ $#list 4 ) {
echo FAIL: list count does not work on a 4 elements list
exit FAIL
}
if( ! ~ $#string 1 ) {
echo FAIL: list count does not work on a single string
exit FAIL
}
echo PASS
exit PASS

View File

@ -46,7 +46,7 @@ extern int strncmp(const char*, const char*, int32_t);
extern char* strpbrk(const char*, const char*);
extern char* strrchr(const char*, int);
extern char* strtok(char*, char*);
extern int32_t strlen(const char*);
extern int strlen(const char*);
extern int32_t strspn(const char*, const char*);
extern int32_t strcspn(const char*, const char*);
extern char* strstr(const char*, const char*);

View File

@ -1,12 +1,19 @@
/*
* Copyright (C) 2016 Giacomo Tesio <giacomo@tesio.it>
* This file is part of Jehanne.
*
* This file is part of the UCB release of Plan 9. It is subject to the license
* terms in the LICENSE file found in the top-level directory of this
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
* part of the UCB release of Plan 9, including this file, may be copied,
* modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* Jehanne is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
@ -15,17 +22,23 @@
char*
getenv(const char *name)
{
int r, f;
int32_t s;
char *ans;
char *p, *ep, ename[100];
int f;
int32_t l;
char path[127+5+1], *value;
if(strchr(name, '/') != nil)
return nil;
snprint(ename, sizeof ename, "/env/%s", name);
if(strcmp(ename+5, name) != 0)
return nil;
f = open(ename, OREAD);
assert(name != nil);
if(name[0]=='\0')
goto BadName;
if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
goto BadName;
if(strchr(name, '/')!=nil)
goto BadName;
snprint(path, sizeof path, "/env/%s", name);
if(strcmp(path+5, name) != 0)
goto BadName;
f = open(path, OREAD);
if(f < 0){
/* try with #e, in case of a previous rfork(RFCNAMEG)
*
@ -34,25 +47,24 @@ getenv(const char *name)
* using #ec when the open in #e fails is both
* slow and not flexible enough.
*/
snprint(ename, sizeof ename, "#e/%s", name);
f = open(ename, OREAD);
snprint(path, sizeof path, "#e/%s", name);
f = open(path, OREAD);
if(f < 0)
return nil;
}
s = seek(f, 0, 2);
ans = malloc(s+1);
if(ans) {
setmalloctag(ans, getcallerpc());
seek(f, 0, 0);
r = read(f, ans, s);
if(r >= 0) {
ep = ans + s - 1;
for(p = ans; p < ep; p++)
if(*p == '\0')
*p = ' ';
ans[s] = '\0';
}
}
l = seek(f, 0, 2);
value = malloc(l+1);
if(value == nil)
goto Done;
setmalloctag(value, getcallerpc());
seek(f, 0, 0);
if(read(f, value, l) >= 0)
value[l] = '\0';
Done:
close(f);
return ans;
return value;
BadName:
werrstr("bad env name: '%s'", name);
return nil;
}

View File

@ -1,44 +1,62 @@
/*
* Copyright (C) 2016 Giacomo Tesio <giacomo@tesio.it>
* This file is part of Jehanne.
*
* This file is part of the UCB release of Plan 9. It is subject to the license
* terms in the LICENSE file found in the top-level directory of this
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
* part of the UCB release of Plan 9, including this file, may be copied,
* modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
* Copyright (C) 2016-2017 Giacomo Tesio <giacomo@tesio.it>
*
* Jehanne is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
int
putenv(const char *name, const char *val)
putenv(const char *name, const char *value)
{
int f;
char ename[100];
int32_t s;
int32_t l;
char path[127+5+1];
if(strchr(name, '/') != nil)
return -1;
snprint(ename, sizeof ename, "/env/%s", name);
if(strcmp(ename+5, name) != 0)
return -1;
f = ocreate(ename, OWRITE, 0664);
assert(name != nil);
assert(value != nil);
if(name[0]=='\0')
goto BadName;
if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
goto BadName;
if(strchr(name, '/')!=nil)
goto BadName;
snprint(path, sizeof path, "/env/%s", name);
if(strcmp(path+5, name) != 0)
goto BadName;
f = ocreate(path, OWRITE, 0664);
if(f < 0){
/* try with #e, in case of a previous rfork(RFCNAMEG)
*/
snprint(ename, sizeof ename, "#e/%s", name);
f = ocreate(ename, OWRITE, 0664);
snprint(path, sizeof path, "#e/%s", name);
f = ocreate(path, OWRITE, 0664);
if(f < 0)
return -1;
return -1;
}
s = strlen(val);
if(write(f, val, s) != s){
l = strlen(value);
if(l > 0 && write(f, value, l) != l){
close(f);
return -1;
}
close(f);
return 0;
BadName:
werrstr("bad env name: '%s'", name);
return -1;
}

View File

@ -10,9 +10,8 @@
#include <u.h>
#include <libc.h>
int32_t
int
strlen(const char *s)
{
return strchr(s, 0) - s;
}