mpv-player / mpv-player/mpv

Lua: utils.format_json throws an error instead of returning nil, error

Open
#10,220 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
37k
Forks
3.5k
Avg merge
1d 10h
Merged PRs (30d)
22

Description

Important Information

mpv version:

mpv 0.34.0-300-g9022b1b51d Copyright © 2000-2022 mpv/MPlayer/mplayer2 projects
 built on Sun May 22 12:11:51 2022
FFmpeg library versions:
   libavutil       57.24.101
   libavcodec      59.28.100
   libavformat     59.24.100
   libswscale      6.6.100
   libavfilter     8.38.100
   libswresample   4.6.100
FFmpeg version: git-2022-05-21-27cffd16

Windows 10 Shinchiro Build

Reproduction steps

Run mpv with the following script:

local utils = require "mp.utils"
print(utils.format_json({ [true] = 1 }))

A boolean is not a valid JSON key, so this will throw an error.

Expected behavior

According to the mpv manual:

Format the given Lua table (or value) as a JSON string and return it. On error, returns nil, error. (Errors usually only happen on value types incompatible with JSON.)

Which means something along the lines of this should be printed:
nil "key must be a string, but got boolean"

Actual behavior

mpv throws an error and the script outright crashes:

image

I can't be sure of this, but I've used format_json in the past and I don't remember this happening then. It's possible I just never gave it an invalid json string, but I find that surprising, so this may have broken sometime in the last couple of years.

Log file

log.txt

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Lua reproduction with mp.utils.format_json({ [true] = 1 }) and then trace the implementation of mp.utils.format_json. The fix is done when invalid JSON keys return nil and an explanatory error string, as documented, instead of throwing and crashing the script.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.