Skip to content

Handle escape sequences in unquoted strings #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"gopkg.in/warnings.v0"
)

var unescape = map[rune]rune{'\\': '\\', '"': '"', 'n': '\n', 't': '\t'}
var unescape = map[rune]rune{'\\': '\\', '"': '"', 'n': '\n', 't': '\t', 'b': '\b'}

// no error: invalid literals should be caught by scanner
func unquote(s string) string {
Expand Down Expand Up @@ -48,7 +48,7 @@ func unquote(s string) string {
return string(u)
}

func read(c *warnings.Collector, callback func(string,string,string,string,bool)error,
func read(c *warnings.Collector, callback func(string, string, string, string, bool) error,
fset *token.FileSet, file *token.File, src []byte) error {
//
var s scanner.Scanner
Expand Down Expand Up @@ -223,7 +223,7 @@ func readInto(config interface{}, fset *token.FileSet, file *token.File,
// (as opposed to set to empty string).
//
// If callback returns an error, ReadWithCallback terminates with an error too.
func ReadWithCallback(reader io.Reader, callback func(string,string,string,string,bool)error) error {
func ReadWithCallback(reader io.Reader, callback func(string, string, string, string, bool) error) error {
src, err := ioutil.ReadAll(reader)
if err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions read_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package gcfg

import (
"bytes"
"fmt"
"math/big"
"os"
"reflect"
"testing"
"bytes"
"strconv"
"testing"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -127,6 +128,8 @@ var readtests = []struct {
{"[section]\nname=\"va\\\"lue\"", &cBasic{Section: cBasicS1{Name: "va\"lue"}}, true},
{"[section]\nname=\"va\\nlue\"", &cBasic{Section: cBasicS1{Name: "va\nlue"}}, true},
{"[section]\nname=\"va\\tlue\"", &cBasic{Section: cBasicS1{Name: "va\tlue"}}, true},
{"[section]\nname=x:\\\\path\\\\", &cBasic{Section: cBasicS1{Name: "x:\\path\\"}}, true},
{"[section]\nname=\\b", &cBasic{Section: cBasicS1{Name: "\b"}}, true},
{"\n[section]\nname=\\", &cBasic{}, false},
{"\n[section]\nname=\\a", &cBasic{}, false},
{"\n[section]\nname=\"val\\a\"", &cBasic{}, false},
Expand Down
8 changes: 4 additions & 4 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *Scanner) scanEscape(val bool) {
switch ch {
case '\\', '"':
// ok
case 'n', 't':
case 'n', 't', 'b':
if val {
break // ok
}
Expand Down Expand Up @@ -232,10 +232,10 @@ loop:
s.next()
}
if s.ch != '\n' {
s.error(offs, "unquoted '\\' must be followed by new line")
break loop
s.scanEscape(true)
} else {
s.next()
}
s.next()
case ch == '"':
inQuote = !inQuote
case ch == '\r':
Expand Down
4 changes: 2 additions & 2 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ var errors = []struct {
{"\"\n", token.STRING, 0, "string not terminated"},
{`="`, token.STRING, 1, "string not terminated"},
{"=\"\n", token.STRING, 1, "string not terminated"},
{"=\\", token.STRING, 1, "unquoted '\\' must be followed by new line"},
{"=\\\r", token.STRING, 1, "unquoted '\\' must be followed by new line"},
{"=\\", token.STRING, 2, "unknown escape sequence"},
{"=\\\r", token.STRING, 3, "unknown escape sequence"},
{`"\z"`, token.STRING, 2, "unknown escape sequence"},
{`"\a"`, token.STRING, 2, "unknown escape sequence"},
{`"\b"`, token.STRING, 2, "unknown escape sequence"},
Expand Down