Fix segfault in applyScrobbleRule

Thanks bob :)
This commit is contained in:
mitteneer 2025-04-27 10:41:42 -04:00
parent be8c1191b0
commit 9df8f9ea12
3 changed files with 20 additions and 13 deletions

View file

@ -32,7 +32,6 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
}; };
var rules = std.ArrayList(Data.Rule).init(allocator); var rules = std.ArrayList(Data.Rule).init(allocator);
defer rules.deinit();
const file_content = try file_read.readToEndAlloc(allocator, 16_000_000); const file_content = try file_read.readToEndAlloc(allocator, 16_000_000);
file_read.close(); file_read.close();

View file

@ -38,8 +38,8 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
}); });
defer rule_file.close(); defer rule_file.close();
const file_content = try rule_file.readToEndAlloc(request.allocator, 16_000_000); const rule_file_content = try rule_file.readToEndAlloc(request.allocator, 16_000_000);
const rule_list = std.json.parseFromSliceLeaky(Data.Rules, request.allocator, file_content, .{}) catch null; const rule_list = std.json.parseFromSliceLeaky(Data.Rules, request.allocator, rule_file_content, .{}) catch null;
switch (source) { switch (source) {
0 => { 0 => {
@ -51,7 +51,7 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
if ((before_limiter or after_limiter) and (scrobble.date > before_limiting_date or scrobble.date < after_limiting_date)) continue :appends; if ((before_limiter or after_limiter) and (scrobble.date > before_limiting_date or scrobble.date < after_limiting_date)) continue :appends;
const formatted_scrobble = if (rule_list) |rl| const formatted_scrobble = if (rule_list) |rl|
rules.applyScrobbleRule(request.allocator, scrobble, rl) try rules.applyScrobbleRule(request.allocator, scrobble, rl)
else else
Data.Scrobble{ Data.Scrobble{
.album = scrobble.album, .album = scrobble.album,
@ -107,9 +107,15 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
continue :appends; continue :appends;
} }
const pre_formatted_scrobble: Data.ImportedScrobble = .{ .track = scrobble.master_metadata_track_name.?, .album = scrobble.master_metadata_album_album_name.?, .artist = scrobble.master_metadata_album_artist_name.?, .date = (try zeit.instant(.{ .source = .{ .iso8601 = scrobble.ts } })).unixTimestamp() * 1000 }; const pre_formatted_scrobble: Data.ImportedScrobble = .{
.track = scrobble.master_metadata_track_name.?,
.album = scrobble.master_metadata_album_album_name.?,
.artist = scrobble.master_metadata_album_artist_name.?,
.date = (try zeit.instant(.{ .source = .{ .iso8601 = scrobble.ts } })).unixTimestamp() * 1000,
};
const formatted_scrobble = if (rule_list) |rl| const formatted_scrobble = if (rule_list) |rl|
rules.applyScrobbleRule(request.allocator, pre_formatted_scrobble, rl) try rules.applyScrobbleRule(request.allocator, pre_formatted_scrobble, rl)
else else
Data.Scrobble{ Data.Scrobble{
.album = pre_formatted_scrobble.album, .album = pre_formatted_scrobble.album,

View file

@ -11,12 +11,14 @@ fn eqlWrapper(haystack: []const u8, needle: []const u8) bool {
return std.mem.eql(u8, haystack, needle); return std.mem.eql(u8, haystack, needle);
} }
pub fn applyScrobbleRule(allocator: std.mem.Allocator, scrobble: Data.ImportedScrobble, rules: Rules) Data.Scrobble { pub fn applyScrobbleRule(allocator: std.mem.Allocator, scrobble: Data.ImportedScrobble, rules: Rules) !Data.Scrobble {
const artists = try allocator.alloc([]const u8, 1);
artists[0] = scrobble.artist;
var output_scrobble = Data.Scrobble{ var output_scrobble = Data.Scrobble{
.track = scrobble.track, .track = scrobble.track,
.artists_track = &[_][]const u8{scrobble.artist}, .artists_track = artists,
.album = scrobble.album, .album = scrobble.album,
.artists_album = &[_][]const u8{scrobble.artist}, .artists_album = artists,
.date = scrobble.date, .date = scrobble.date,
}; };
@ -47,10 +49,10 @@ pub fn applyScrobbleRule(allocator: std.mem.Allocator, scrobble: Data.ImportedSc
switch (act.action_on) { switch (act.action_on) {
.album, .track => unreachable, .album, .track => unreachable,
inline else => |on| { inline else => |on| {
// I have decided an error won't happen :) try al.appendSlice(@field(output_scrobble, @tagName(on)));
al.appendSlice(@field(output_scrobble, @tagName(on))) catch unreachable; try al.append(act.action_txt);
al.append(act.action_txt) catch unreachable; const list = try al.toOwnedSlice();
@field(output_scrobble, @tagName(on)) = al.items; @field(output_scrobble, @tagName(on)) = list;
}, },
//else => { //else => {
// std.log.debug("Adding artists doesn't work yet", .{}); // std.log.debug("Adding artists doesn't work yet", .{});