runtime-tools icon indicating copy to clipboard operation
runtime-tools copied to clipboard

Bug in createEnvCacheMap()

Open muhmud opened this issue 1 month ago • 0 comments

When using NewFromTemplate(), or other similar functions, a call is made to createEnvCacheMap() to populate a map of the environment variables, used for later lookups:

func createEnvCacheMap(env []string) map[string]int {
	envMap := make(map[string]int, len(env))
	for i, val := range env {
		envMap[val] = i
	}
	return envMap
}

Unfortunately, this is keying the map on the name/value pair for the environment variable, rather than just the name, so when environment variables are later added with the same name, the updated variables are not added correctly:

// addEnv looks through adds ENV to the Process and checks envMap for
// any duplicates
// This is called by both AddMultipleProcessEnv and AddProcessEnv
func (g *Generator) addEnv(env, key string) {
	if idx, ok := g.envMap[key]; ok {
		// The ENV exists in the cache, so change its value in g.Config.Process.Env
		g.Config.Process.Env[idx] = env
	} else {
		// else the env doesn't exist, so add it and add it's index to g.envMap
		g.Config.Process.Env = append(g.Config.Process.Env, env)
		g.envMap[key] = len(g.Config.Process.Env) - 1
	}
}

Here, the assumption is made that envMap is keyed by the name only.

Really, createEnvCacheMap() should perform a strings.SplitN(val, "=", 2) and use the name only for its key.

muhmud avatar Nov 13 '25 09:11 muhmud