Commit 1a9805ad authored by Robert Griesemer's avatar Robert Griesemer

- avoid division-by-zero crash in tabwriter

- correct tabwidth argument for some tabwriter test cases
- catch negative tabwidth flag in gofmt w/o crashing

R=rsc
http://go/go-review/1026022
parent 9bebe741
...@@ -153,6 +153,10 @@ func walkDir(path string) { ...@@ -153,6 +153,10 @@ func walkDir(path string) {
func main() { func main() {
flag.Usage = usage; flag.Usage = usage;
flag.Parse(); flag.Parse();
if *tabwidth < 0 {
fmt.Fprintf(os.Stderr, "negative tabwidth %d\n", *tabwidth);
os.Exit(2);
}
if flag.NArg() == 0 { if flag.NArg() == 0 {
if err := processFile("/dev/stdin"); err != nil { if err := processFile("/dev/stdin"); err != nil {
......
...@@ -229,6 +229,10 @@ func (b *Writer) write0(buf []byte) os.Error { ...@@ -229,6 +229,10 @@ func (b *Writer) write0(buf []byte) os.Error {
var newline = []byte{'\n'} var newline = []byte{'\n'}
func (b *Writer) writePadding(textw, cellw int) os.Error { func (b *Writer) writePadding(textw, cellw int) os.Error {
if b.cellwidth == 0 {
return nil;
}
if b.padbytes[0] == '\t' { if b.padbytes[0] == '\t' {
// make cell width a multiple of cellwidth // make cell width a multiple of cellwidth
cellw = ((cellw + b.cellwidth - 1) / b.cellwidth) * b.cellwidth; cellw = ((cellw + b.cellwidth - 1) / b.cellwidth) * b.cellwidth;
......
...@@ -283,7 +283,7 @@ var tests = []entry{ ...@@ -283,7 +283,7 @@ var tests = []entry{
entry{ entry{
"9a", "9a",
0, 0, '.', 0, 1, 0, '.', 0,
"1\t2\t3\t4\n" "1\t2\t3\t4\n"
"11\t222\t3333\t44444\n", "11\t222\t3333\t44444\n",
...@@ -293,7 +293,7 @@ var tests = []entry{ ...@@ -293,7 +293,7 @@ var tests = []entry{
entry{ entry{
"9b", "9b",
0, 0, '.', FilterHTML, 1, 0, '.', FilterHTML,
"1\t2<!---\f--->\t3\t4\n" // \f inside HTML is ignored "1\t2<!---\f--->\t3\t4\n" // \f inside HTML is ignored
"11\t222\t3333\t44444\n", "11\t222\t3333\t44444\n",
...@@ -303,7 +303,7 @@ var tests = []entry{ ...@@ -303,7 +303,7 @@ var tests = []entry{
entry{ entry{
"9c", "9c",
0, 0, '.', 0, 1, 0, '.', 0,
"1\t2\t3\t4\f" // \f causes a newline and flush "1\t2\t3\t4\f" // \f causes a newline and flush
"11\t222\t3333\t44444\n", "11\t222\t3333\t44444\n",
...@@ -313,7 +313,7 @@ var tests = []entry{ ...@@ -313,7 +313,7 @@ var tests = []entry{
entry{ entry{
"9c debug", "9c debug",
0, 0, '.', Debug, 1, 0, '.', Debug,
"1\t2\t3\t4\f" // \f causes a newline and flush "1\t2\t3\t4\f" // \f causes a newline and flush
"11\t222\t3333\t44444\n", "11\t222\t3333\t44444\n",
...@@ -445,7 +445,7 @@ var tests = []entry{ ...@@ -445,7 +445,7 @@ var tests = []entry{
entry{ entry{
"14", "14",
0, 2, ' ', AlignRight, 1, 2, ' ', AlignRight,
".0\t.3\t2.4\t-5.1\t\n" ".0\t.3\t2.4\t-5.1\t\n"
"23.0\t12345678.9\t2.4\t-989.4\t\n" "23.0\t12345678.9\t2.4\t-989.4\t\n"
"5.1\t12.0\t2.4\t-7.0\t\n" "5.1\t12.0\t2.4\t-7.0\t\n"
...@@ -463,7 +463,7 @@ var tests = []entry{ ...@@ -463,7 +463,7 @@ var tests = []entry{
entry{ entry{
"14 debug", "14 debug",
0, 2, ' ', AlignRight | Debug, 1, 2, ' ', AlignRight | Debug,
".0\t.3\t2.4\t-5.1\t\n" ".0\t.3\t2.4\t-5.1\t\n"
"23.0\t12345678.9\t2.4\t-989.4\t\n" "23.0\t12345678.9\t2.4\t-989.4\t\n"
"5.1\t12.0\t2.4\t-7.0\t\n" "5.1\t12.0\t2.4\t-7.0\t\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment