Skip to content

Commit bb08b9d

Browse files
committed
fix(config): validate configuration activation
SetConfig and ReadConfig returned success when libGammu rejected the active configuration count, deferring the failure until initialization. Closes gammu/gammu#414
1 parent 905c7c8 commit bb08b9d

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

gammu/src/gammu.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,20 @@ StateMachine_GetConfig(StateMachineObject *self, PyObject *args, PyObject *kwds)
554554
"UseGlobalDebugFile", Config->UseGlobalDebugFile);
555555
}
556556

557+
static int
558+
StateMachine_ActivateConfig(StateMachineObject *self, int section)
559+
{
560+
if (GSM_GetConfigNum(self->s) <= section) {
561+
GSM_SetConfigNum(self->s, section + 1);
562+
if (GSM_GetConfigNum(self->s) <= section) {
563+
PyErr_Format(PyExc_ValueError, "Maximal configuration storage exceeded");
564+
return 0;
565+
}
566+
}
567+
568+
return 1;
569+
}
570+
557571
static char StateMachine_SetConfig__doc__[] =
558572
"SetConfig(Section, Values)\n\n"
559573
"Sets specified config section.\n\n"
@@ -688,8 +702,8 @@ StateMachine_SetConfig(StateMachineObject *self, PyObject *args, PyObject *kwds)
688702
}
689703
}
690704

691-
/* Tell Gammu we have configured another section */
692-
GSM_SetConfigNum(self->s, section + 1);
705+
if (!StateMachine_ActivateConfig(self, section))
706+
return NULL;
693707

694708
Py_RETURN_NONE;
695709
}
@@ -746,8 +760,10 @@ StateMachine_ReadConfig(StateMachineObject *self, PyObject *args, PyObject *kwds
746760
}
747761
Config->UseGlobalDebugFile = FALSE;
748762

749-
/* Tell Gammu we have configured another section */
750-
GSM_SetConfigNum(self->s, dst + 1);
763+
if (!StateMachine_ActivateConfig(self, dst)) {
764+
INI_Free(cfg);
765+
return NULL;
766+
}
751767

752768
INI_Free(cfg);
753769

test/test_config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@
3030

3131

3232
class ConfigTest(unittest.TestCase):
33+
def test_config_sections(self) -> None:
34+
with tempfile.TemporaryDirectory() as temp_dir:
35+
config_file = Path(temp_dir) / "gammurc"
36+
config_file.write_text(
37+
"[gammu4]\nconnection = none\ndevice = /dev/null\n",
38+
encoding="utf-8",
39+
)
40+
41+
state_machine = gammu.StateMachine()
42+
state_machine.ReadConfig(Section=4, Filename=str(config_file))
43+
state_machine.SetConfig(0, state_machine.GetConfig(4))
44+
45+
cfg = state_machine.GetConfig(4)
46+
assert cfg["Connection"] == "none"
47+
assert cfg["Device"] == "/dev/null"
48+
49+
with pytest.raises(
50+
ValueError,
51+
match="Requested configuration not available",
52+
):
53+
state_machine.GetConfig(100)
54+
3355
def test_config_bool(self) -> None:
3456
state_machine = gammu.StateMachine()
3557
state_machine.SetConfig(

0 commit comments

Comments
 (0)