"Index out of range" after a couple of hundred iterations.

xuanbao · · 478 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello <a href="/r/golang" rel="nofollow">/r/golang</a>,</p> <p>I&#39;m learning Golang and my current project is to read a UFW log file and display the source IP address and the destination port number. The problem is that I get the following error:</p> <pre><code>panic: runtime error: index out of range goroutine 5 [running]: panic(0x5231e0, 0xc820010140) /usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6 main.countIps(0xc82002e020, 0xc820014300, 0xc8200103f0) /home/ruben/Programming/Golang/src/github.com/J0holo/ufwLogReader/ufwLogReader.go:65 +0x290 created by main.main /home/ruben/Programming/Golang/src/github.com/J0holo/ufwLogReader/ufwLogReader.go:41 +0x2e4 </code></pre> <p>The debugger shows that it only occurs after a couple of hundred loops in countIps(), so it&#39;s not instantly. The debugger only spits out a assembly file and in the debugger screen a list of IPs and port numbers that it has found.</p> <p><a href="https://gist.github.com/j0holo/d3ab4c33cd33305acd20e99f24d04029" rel="nofollow">The source code</a> which also contains an example of a line of an UFW log file.</p> <p>I already checked if the maps should be accessed by-reference but <a href="http://stackoverflow.com/a/28384528" rel="nofollow">they seem to work like arrays</a> in C. I have no idea how to fix this error with my limited Go knowledge. Plz help, thanks!</p> <p>EDIT: <a href="/u/the_jester" rel="nofollow">/u/the_jester</a> and <a href="/u/hipone" rel="nofollow">/u/hipone</a> just told me to RTFM. Ty guys. TIL: &#34;&#34; != nil in Go</p> <hr/>**评论:**<br/><br/>the_jester: <pre><p>Note that <a href="https://golang.org/pkg/regexp/#Regexp.FindStringSubmatch" rel="nofollow">FindStringSubmatch can return nil</a>, but you reference its return value without checking for nil on line 66.</p></pre>j0holo: <pre><p>Doh, I compared it as a string not as a string array. Just tested it in the go playground.</p> <p>Will test it tommorow in my program, thanks anyway.</p></pre>hipone: <pre><blockquote> <p><a href="https://golang.org/pkg/regexp/#Regexp.FindStringSubmatch" rel="nofollow">https://golang.org/pkg/regexp/#Regexp.FindStringSubmatch</a></p> <p>A return value of nil indicates no match.</p> </blockquote> <p>And you don&#39;t check for nil <a href="https://gist.github.com/j0holo/d3ab4c33cd33305acd20e99f24d04029#file-ufwlogreader-go-L65" rel="nofollow">here</a>.</p></pre>breml: <pre><p>Maps in go are not concurrency safe. As you access ipCounds and portCounts from go routines, you should protect your access to thous maps. Edited: The go race detector could help you to localize the race condition.</p></pre>j0holo: <pre><p>Already fixed that problem, the code is now on <a href="https://github.com/j0holo/ufwLogReader" rel="nofollow">github</a>.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

478 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传