While I'm parsing alliance feed in a Sinatra/Ruby app I get the error:
/opt/rh/ruby200/root/usr/share/ruby/net/http/response.rb:368: [BUG]
Segmentation fault ruby 2.0.0p645 (2015-04-13) [x86_64-linux]
I'm wondering if this is a bug with Ruby or something wrong with the code, and if so, what could I do to fix it?
Link to the error
This is the code for the parsing alliance feed:
feeds.each { |name, hash|
puts "=== PARSING #{name.upcase} FEED ==="
start = Time.now
open(hash[:url]) { |feed|
send(hash[:action], feed)
}
duration = Time.now - start
puts "Feed syndication completed in #{duration.to_s} seconds."
puts
}
# Close DB connection
puts "Disconnecting"
@db.disconnect
end
def parseAllianceData(xml)
start = Time.now
allianceData = XMLObject.new xml
duration = Time.now - start
puts "XML parsed in #{duration.to_s} seconds."
puts "Alliances found: #{allianceData.alliances.count}"
@db[:feeds].insert(
:generated_at => allianceData.server.datagenerationdatetime,
:type => "Alliance",
:is_current => true)
start = Time.now
allianceData.alliances.each { |alliance|
capital_last_moved_at = (alliance.alliancecapitallastmoved rescue nil)
taxrate_last_changed_at = (alliance.alliancetaxratelastchanged rescue nil)
@db[:alliance].insert(
:id => alliance.alliance[:id],
:ticker => alliance.allianceticker,
:name => alliance.alliance,
:founded_at => alliance.foundeddatetime,
:founded_by_player_id => alliance.foundedbyplayerid[:id],
:capital_town_id => alliance.alliancecapitaltownid[:id],
:member_count => alliance.membercount,
:total_population => (alliance.totalpopulation rescue 0),
:tax_rate => (alliance.alliancetaxrate.to_i) / 100.0,
:tax_rate_last_changed_at => taxrate_last_changed_at,
:capital_town_last_moved_at => capital_last_moved_at)
alliance.roles.each { |role|
@db[:alliance_roles].insert(
:id => role.role[:id],
:name => role.role,
:alliance_id => alliance.alliance[:id],
:hierarchy_id => role.heirarchy[:id])
}
}
duration = Time.now - start
puts "Database populated in #{duration.to_s} seconds."
I spot one dangerous line of code in your sample:
send(hash[:action], feed)
It takes some string from external source (hash[:action]
) and turns it into a method call. It is very dangerous, because you never know what string you will get. There could be a string there that cannot be made into a method call so Ruby crashes.
I would suggest checking for all supported actions and calling methods explicitly. You can do it with a case
statement, for example.
action = hash[:action]
case action
when 'action1'
call_method1
when 'action2'
call_method2
else
puts "unsupported action: #{action}"
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With