• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util-lua-bytevarlib.c
1
/* Copyright (C) 2025 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
#include "suricata-common.h"
19
#include "detect-byte.h"
20
#include "util-lua-common.h"
21
#include "util-lua-bytevarlib.h"
22
#include "util-lua.h"
23
#include "detect-lua.h"
24
#include "detect-lua-extensions.h"
25

26
#include "lauxlib.h"
27

28
static const char suricata_bytevar_mt[] = "suricata:bytevar:mt";
29

30
static DetectLuaData *GetLuaData(lua_State *luastate)
UNCOV
31
{
×
UNCOV
32
    DetectLuaData *ld;
×
UNCOV
33
    lua_pushlightuserdata(luastate, (void *)&luaext_key_ld);
×
UNCOV
34
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
35
    ld = lua_touserdata(luastate, -1);
×
UNCOV
36
    return ld;
×
UNCOV
37
}
×
38

39
static int LuaBytevarMap(lua_State *L)
UNCOV
40
{
×
UNCOV
41
    const Signature *s = lua_touserdata(L, -2);
×
UNCOV
42
    const char *name = luaL_checkstring(L, -1);
×
UNCOV
43
    DetectLuaData *ld = GetLuaData(L);
×
44

45
    /* Is this name already mapped? */
UNCOV
46
    for (uint16_t i = 0; i < ld->bytevars; i++) {
×
UNCOV
47
        if (strcmp(ld->bytevar[i].name, name) == 0) {
×
48
            lua_pushinteger(L, ld->bytevar[i].id);
×
49
            return 1;
×
50
        }
×
UNCOV
51
    }
×
52

UNCOV
53
    if (ld->bytevars == DETECT_LUA_MAX_BYTEVARS) {
×
54
        luaL_error(L, "too many bytevars mapped");
×
55
    }
×
56

UNCOV
57
    DetectByteIndexType idx;
×
UNCOV
58
    if (!DetectByteRetrieveSMVar(name, s, -1, &idx)) {
×
59
        return luaL_error(L, "unknown byte_extract or byte_math variable: %s", name);
×
60
    }
×
61

UNCOV
62
    ld->bytevar[ld->bytevars].name = SCStrdup(name);
×
UNCOV
63
    if (ld->bytevar[ld->bytevars].name == NULL) {
×
64
        luaL_error(L, "failed to allocate memory for bytevar name: %s", name);
×
65
    }
×
UNCOV
66
    ld->bytevar[ld->bytevars++].id = idx;
×
67

UNCOV
68
    return 1;
×
UNCOV
69
}
×
70

71
static int LuaBytevarGet(lua_State *L)
UNCOV
72
{
×
UNCOV
73
    const char *name = luaL_checkstring(L, 1);
×
UNCOV
74
    DetectLuaData *ld = GetLuaData(L);
×
UNCOV
75
    if (ld == NULL) {
×
76
        return luaL_error(L, "internal error: no lua data");
×
77
    }
×
78

UNCOV
79
    for (uint16_t i = 0; i < ld->bytevars; i++) {
×
UNCOV
80
        if (strcmp(ld->bytevar[i].name, name) == 0) {
×
UNCOV
81
            uint32_t *bytevar_id = lua_newuserdata(L, sizeof(*bytevar_id));
×
UNCOV
82
            *bytevar_id = ld->bytevar[i].id;
×
UNCOV
83
            luaL_getmetatable(L, suricata_bytevar_mt);
×
UNCOV
84
            lua_setmetatable(L, -2);
×
UNCOV
85
            return 1;
×
UNCOV
86
        }
×
UNCOV
87
    }
×
88

89
    return luaL_error(L, "unknown bytevar: %s", name);
×
UNCOV
90
}
×
91

92
static int LuaBytevarValue(lua_State *L)
UNCOV
93
{
×
UNCOV
94
    uint32_t *bytevar_id = luaL_checkudata(L, 1, suricata_bytevar_mt);
×
UNCOV
95
    DetectEngineThreadCtx *det_ctx = LuaStateGetDetCtx(L);
×
UNCOV
96
    if (det_ctx == NULL) {
×
97
        return LuaCallbackError(L, "internal error: no det_ctx");
×
98
    }
×
UNCOV
99
    lua_pushinteger(L, det_ctx->byte_values[*bytevar_id]);
×
UNCOV
100
    return 1;
×
UNCOV
101
}
×
102

103
static const luaL_Reg bytevarlib[] = {
104
    // clang-format off
105
    { "map", LuaBytevarMap, },
106
    { "get", LuaBytevarGet, },
107
    { NULL, NULL, },
108
    // clang-format on
109
};
110

111
static const luaL_Reg bytevarmt[] = {
112
    // clang-format off
113
    { "value", LuaBytevarValue, },
114
    { NULL, NULL, },
115
    // clang-format on
116
};
117

118
int LuaLoadBytevarLib(lua_State *L)
UNCOV
119
{
×
UNCOV
120
    luaL_newmetatable(L, suricata_bytevar_mt);
×
UNCOV
121
    lua_pushvalue(L, -1);
×
UNCOV
122
    lua_setfield(L, -2, "__index");
×
UNCOV
123
    luaL_setfuncs(L, bytevarmt, 0);
×
124

UNCOV
125
    luaL_newlib(L, bytevarlib);
×
UNCOV
126
    return 1;
×
UNCOV
127
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc