Skip to content

extractVarValue assumes a quote is escaped if a backslash precedes it, even if the backslash is escaped #225

@monerowner

Description

@monerowner

Given a .env with only one line:
FOO = "\\"

godotenv.Read(".env") will return an error because the prevChar == '\\', even though it's escaped.

// lookup quoted string terminator
for i := 1; i < len(src); i++ {
	if char := src[i]; char != quote {
		continue
	}

	// skip escaped quote symbol (\" or \', depends on quote)
	if prevChar := src[i-1]; prevChar == '\\' {
		continue
	}

	// trim quotes
	trimFunc := isCharFunc(rune(quote))
	value = string(bytes.TrimLeftFunc(bytes.TrimRightFunc(src[0:i], trimFunc), trimFunc))
	if quote == prefixDoubleQuote {
		// unescape newlines for double quote (this is compat feature)
		// and expand environment variables
		value = expandVariables(expandEscapes(value), vars)
	}

	return value, src[i+1:], nil
}

extractVarValue should instead check whether the quote symbol is preceded by an odd number of backslashes (escaped) or an even number (not escaped).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions