dougai8673 2018-01-16 18:53 采纳率: 100%
浏览 69
已采纳

紧急:运行时错误:索引超出范围[已恢复]

In my code panic error shown after Modify if j == len(remark) to if j == len(remark) && z > 0

Error is :

--- FAIL: TestHey (0.00s)
panic: runtime error: index out of range [recovered]
        panic: runtime error: index out of range

goroutine 5 [running]:
testing.tRunner.func1(0xc04207a0f0)
        C:/Go/src/testing/testing.go:711 +0x2d9
panic(0x526700, 0x5f57c0)
        C:/Go/src/runtime/panic.go:491 +0x291
_/C_/Exercism/Go/bob.Hey(0x0, 0x0, 0x54bda2, 0x5)
        C:/Exercism/Go/bob/bob.go:23 +0x26a
_/C_/Exercism/Go/bob.TestHey(0xc04207a0f0)
        C:/Exercism/Go/bob/bob_test.go:8 +0xd3
testing.tRunner(0xc04207a0f0, 0x555e98)
        C:/Go/src/testing/testing.go:746 +0xd7
created by testing.(*T).Run
        C:/Go/src/testing/testing.go:789 +0x2e5
exit status 2
FAIL    _/C_/Exercism/Go/bob    0.072s

bob.go file :

package bob

// Hey should have a comment documenting it.
func Hey(remark string) string {
    j := 0
    z := 0
    for i := 0; i < len(remark); i++ {
        te := remark[i]
        if te < 'a' || te > 'z' {
            j++
        }
        if te >= 'A' && te <= 'Z' {
            z++
        }
    }
    if j == len(remark) && z > 0 {
        if remark[len(remark)-1] == '?' {
            return "Calm down, I know what I'm doing!"
        } else {
            return "Whoa, chill out!"
        }
    }
    if remark[len(remark)-1] == '?' {
        return "Sure."
    }
    j = 0
    for i := 0; i < len(remark); i++ {
        if string(remark[i]) == string(9) || string(remark[i]) == string(10) || string(remark[i]) == string(13) || string(remark[i]) == string(32) {
            j++
        }
    }
    if j == len(remark) {
        return "Fine. Be that way!"
    }
    return "Whatever."
}

bob_test file:

package bob

import "testing"
import "fmt"

func TestHey(t *testing.T) {
    for _, tt := range testCases {
        actual := Hey(tt.input)
        if actual != tt.expected {
            msg := `
    ALICE (%s): %q
    BOB: %s

    Expected Bob to respond: %s`
            t.Fatalf(msg, tt.description, tt.input, actual, tt.expected)
        }
    }
}

func BenchmarkHey(b *testing.B) {
    for _, tt := range testCases {
        for i := 0; i < b.N; i++ {
            Hey(tt.input)
            fmt.Println(i)
        }
    }
}

cases_test file:

package bob

// Source: exercism/problem-specifications
// Commit: 6dc2014 bob: apply "input" policy
// Problem Specifications Version: 1.2.0

var testCases = []struct {
    description string
    input       string
    expected    string
}{
    {
        "stating something",
        "Tom-ay-to, tom-aaaah-to.",
        "Whatever.",
    },
    {
        "shouting",
        "WATCH OUT!",
        "Whoa, chill out!",
    },
    {
        "shouting gibberish",
        "FCECDFCAAB",
        "Whoa, chill out!",
    },
    {
        "asking a question",
        "Does this cryogenic chamber make me look fat?",
        "Sure.",
    },
    {
        "asking a numeric question",
        "You are, what, like 15?",
        "Sure.",
    },
    {
        "asking gibberish",
        "fffbbcbeab?",
        "Sure.",
    },
    {
        "talking forcefully",
        "Let's go make out behind the gym!",
        "Whatever.",
    },
    {
        "using acronyms in regular speech",
        "It's OK if you don't want to go to the DMV.",
        "Whatever.",
    },
    {
        "forceful question",
        "WHAT THE HELL WERE YOU THINKING?",
        "Calm down, I know what I'm doing!",
    },
    {
        "shouting numbers",
        "1, 2, 3 GO!",
        "Whoa, chill out!",
    },
    {
        "only numbers",
        "1, 2, 3",
        "Whatever.",
    },
    {
        "question with only numbers",
        "4?",
        "Sure.",
    },
    {
        "shouting with special characters",
        "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!",
        "Whoa, chill out!",
    },
    {
        "shouting with no exclamation mark",
        "I HATE YOU",
        "Whoa, chill out!",
    },
    {
        "statement containing question mark",
        "Ending with ? means a question.",
        "Whatever.",
    },
    {
        "non-letters with question",
        ":) ?",
        "Sure.",
    },
    {
        "prattling on",
        "Wait! Hang on. Are you going to be OK?",
        "Sure.",
    },
    {
        "silence",
        "",
        "Fine. Be that way!",
    },
    {
        "prolonged silence",
        "          ",
        "Fine. Be that way!",
    },
    {
        "alternate silence",
        "\t\t\t\t\t\t\t\t\t\t",
        "Fine. Be that way!",
    },
    {
        "multiple line question",
        "
Does this cryogenic chamber make me look fat?
no",
        "Whatever.",
    },
    {
        "starting with whitespace",
        "         hmmmmmmm...",
        "Whatever.",
    },
    {
        "ending with whitespace",
        "Okay if like my  spacebar  quite a bit?   ",
        "Sure.",
    },
    {
        "other whitespace",
        "
 \t",
        "Fine. Be that way!",
    },
    {
        "non-question ending with whitespace",
        "This is a statement ending with whitespace      ",
        "Whatever.",
    },
}
  • 写回答

1条回答 默认 最新

  • dps69208 2018-01-16 20:34
    关注

    One of your test cases is:

    {
        "silence",
        "",
        "Fine. Be that way!",
    },
    

    This calls Hey(""), meaning that len(remark) == 0.

    The panic occurs at L23:

    if remark[len(remark)-1] == '?' {
    

    With the provided input, that turns into remark[-1] which is definitely out of range and causes a panic.

    You should sanitize your inputs which includes handling empty strings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据