From 2acf400942f0c7039fcc35563accc3943257e6a5 Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Mon, 26 Jul 2021 14:04:19 +0200 Subject: [PATCH] Detect writing invalid sections And display a proper error, instead of some random pairs-error --- inifile.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/inifile.lua b/inifile.lua index 4dc31fa..d666d28 100644 --- a/inifile.lua +++ b/inifile.lua @@ -140,12 +140,22 @@ function inifile.save(name, t, backend) table.insert(contents, ("%s=%s"):format(key, tostring(value))) end + local function tableLike(value) + local function index() + return value[1] + end + + return pcall(index) and pcall(next, value) + end + local function writesection(section, order) local s = t[section] -- Discard if it doesn't exist (anymore) if not s then return end table.insert(contents, ("[%s]"):format(section)) + assert(tableLike(s), ("Invalid section %s: not table-like"):format(section)) + -- Write our comments out again, sadly we have only achieved -- section-accuracy so far if comments and comments[section] then