Skip to content

Commit

Permalink
Remove the buffer argument from loc_getenv
Browse files Browse the repository at this point in the history
1.Avoid to allocate the big buffer on the caller stack
2.The temporary buffer is only used on cygwin

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I8bd325624a2f9b8cd1f791f0c07fc76d02158e1d
  • Loading branch information
xiaoxiang781216 committed Jun 28, 2021
1 parent 95ae7d7 commit 056ea22
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
18 changes: 10 additions & 8 deletions compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,16 @@ char *strsep(char **string_p, const char *delim)
/*
* Local getenv which handles some portability stuff.
*/
char *loc_getenv(const char *var, char *buf, const int buf_size,
const int stay_safe)
{
#undef loc_getenv
#if defined(__CYGWIN__) && HAVE_GETENVIRONMENTVARIABLEA
/* use this function instead of getenv */
GetEnvironmentVariableA(var, buf, buf_size);
return buf;
#else /* ! __CYGWIN__ */
char *loc_getenv(const char *var, char *buf, const int buf_size)
{
int ret = GetEnvironmentVariableA(var, buf, buf_size);
return ret > 0 && ret < buf_size ? buf : NULL;
}
#else
char *loc_getenv(const char *var, const int stay_safe)
{
#if GETENV_SAFE == 0
if (stay_safe) {
/* oh, well. no idea how to get the environmental variables */
Expand All @@ -484,5 +486,5 @@ char *loc_getenv(const char *var, char *buf, const int buf_size,
#endif /* GETENV_SAFE == 0 */
/* get the options flag */
return getenv(var);
#endif /* ! __CYGWIN__ */
}
#endif /* ! __CYGWIN__ */
15 changes: 13 additions & 2 deletions compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,20 @@ char *strsep(char **string_p, const char *delim);
/*
* Local getenv which handles some portability stuff.
*/
#if defined(__CYGWIN__) && HAVE_GETENVIRONMENTVARIABLEA
extern
char *loc_getenv(const char *var, char *buf, const int buf_size,
const int stay_safe);
char *loc_getenv(const char *var, char *buf, const int buf_size);

#define loc_getenv(var, stay_safe, val) \
char buf##__LINE__[256]; \
*(val) = loc_getenv(var, buf##__LINE__, sizeof(buf##__LINE__))
#else
extern
char *loc_getenv(const char *var, const int stay_safe);

#define loc_getenv(var, stay_safe, val) \
*(val) = loc_getenv(var, stay_safe)
#endif /* ! __CYGWIN__ */

/*<<<<<<<<<< This is end of the auto-generated output from fillproto. */

Expand Down
15 changes: 5 additions & 10 deletions dmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,8 @@ static void choose_shell(void)
{
const char *shell, *shell_p;
int shell_c;
char env_buf[256];

shell = loc_getenv(SHELL_ENVIRON, env_buf, sizeof(env_buf), 0);
loc_getenv(SHELL_ENVIRON, 0, &shell);
if (shell == NULL) {
/* oh well, we just guess on c-shell */
cshell_b = 1;
Expand Down Expand Up @@ -504,7 +503,6 @@ static long find_tag(const long debug_value, const char *tag_find,
const char *home_p;
int ret;
long new_debug = 0;
char env_buf[256];

/* do we need to have a home variable? */
if (inpath == NULL) {
Expand All @@ -523,7 +521,7 @@ static long find_tag(const long debug_value, const char *tag_find,
}
else {
/* find our home directory */
home_p = loc_getenv(HOME_ENVIRON, env_buf, sizeof(env_buf), 0);
loc_getenv(HOME_ENVIRON, 0, &home_p);
if (home_p == NULL) {
loc_fprintf(stderr, "%s: could not find variable '%s'\n",
argv_program, HOME_ENVIRON);
Expand Down Expand Up @@ -620,7 +618,6 @@ static void list_tags(void)
const char *home_p;
long new_debug = 0;
FILE *rc_file;
char env_buf[256];

/* do we need to have a home variable? */
if (inpath == NULL) {
Expand All @@ -630,7 +627,7 @@ static void list_tags(void)
if (rc_file == NULL) {

/* if no file in current directory, try home directory */
home_p = loc_getenv(HOME_ENVIRON, env_buf, sizeof(env_buf), 0);
loc_getenv(HOME_ENVIRON, 0, &home_p);
if (home_p == NULL) {
loc_fprintf(stderr, "%s: could not find variable '%s'\n",
argv_program, HOME_ENVIRON);
Expand Down Expand Up @@ -703,10 +700,9 @@ static void dump_current(void)
unsigned long addr_count;
int lock_on, loc_start_line;
unsigned int flags;
char env_buf[256];

/* get the options flag */
env_str = loc_getenv(OPTIONS_ENVIRON, env_buf, sizeof(env_buf), 0);
loc_getenv(OPTIONS_ENVIRON, 0, &env_str);
if (env_str == NULL) {
env_str = "";
}
Expand Down Expand Up @@ -858,7 +854,6 @@ int main(int argc, char **argv)
int lock_on;
int loc_start_line;
unsigned int flags;
char env_buf[256];

argv_help_string = "Sets dmalloc library env variables. Also try --usage.";
argv_version_string = dmalloc_version;
Expand Down Expand Up @@ -897,7 +892,7 @@ int main(int argc, char **argv)
}

/* get the current debug information from the env variable */
env_str = loc_getenv(OPTIONS_ENVIRON, env_buf, sizeof(env_buf), 0);
loc_getenv(OPTIONS_ENVIRON, 0, &env_str);
if (env_str == NULL) {
env_str = "";
}
Expand Down
6 changes: 2 additions & 4 deletions dmalloc_argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2710,7 +2710,6 @@ static int do_env_args(argv_t *args, argv_t **queue_list,
{
int env_c, env_n;
char **vect_p, env_name[256], *environ_p;
char env_buf[256];

/* create the env variable */
(void)loc_snprintf(env_name, sizeof(env_name), ENVIRON_FORMAT, argv_program);
Expand All @@ -2722,7 +2721,7 @@ static int do_env_args(argv_t *args, argv_t **queue_list,
}
}

environ_p = loc_getenv(env_name, env_buf, sizeof(env_buf), 0);
loc_getenv(env_name, 0, &environ_p);
if (environ_p == NULL) {
return NOERROR;
}
Expand Down Expand Up @@ -2761,7 +2760,6 @@ static int process_env(void)
static int done_b = ARGV_FALSE;
char *env_val, *tok_p, *env_p;
int len;
char env_buf[256];

/* make sure we only do this once */
if (done_b) {
Expand All @@ -2771,7 +2769,7 @@ static int process_env(void)
done_b = ARGV_TRUE;

/* get the argv information */
env_val = loc_getenv(GLOBAL_NAME, env_buf, sizeof(env_buf), 0);
loc_getenv(GLOBAL_NAME, 0, &env_val);
if (env_val == NULL) {
return NOERROR;
}
Expand Down
24 changes: 17 additions & 7 deletions user_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ static int dmalloc_startup(const char *debug_str)
{
static int some_up_b = 0;
const char *env_str;
char env_buf[256];

/* have we started already? */
if (enabled_b) {
Expand All @@ -356,15 +355,14 @@ static int dmalloc_startup(const char *debug_str)
#endif
#endif

/* process the environmental variable(s) */
if (debug_str == NULL) {
env_str = loc_getenv(OPTIONS_ENVIRON, env_buf, sizeof(env_buf),
1 /* stay safe */);
loc_getenv(OPTIONS_ENVIRON, 1 /* stay safe */, &env_str);
process_environ(env_str);
}
else {
env_str = debug_str;
process_environ(debug_str);
}
/* process the environmental variable(s) */
process_environ(env_str);

/*
* Tune the environment here. If we have a start-file,
Expand Down Expand Up @@ -1516,10 +1514,22 @@ unsigned int dmalloc_debug_current(void)
*/
char *dmalloc_debug_current_env(char *env_buf, const int env_buf_size)
{
const char *env_str;

if (! enabled_b) {
(void)dmalloc_startup(NULL /* no options string */);
}
return loc_getenv(OPTIONS_ENVIRON, env_buf, env_buf_size, 0);

loc_getenv(OPTIONS_ENVIRON, 0, &env_str);
if (env_str) {
strncpy(env_buf, env_str, env_buf_size);
env_buf[env_buf_size - 1] = '\0';
}
else {
env_buf[0] = '\0';
}

return env_buf;
}

/*
Expand Down

0 comments on commit 056ea22

Please sign in to comment.