<commit_before>#include <stdlib.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <format.hpp>

#include "dhcplua.hpp"
#include "leasestore.hpp"

extern "C" {
#include "options.h"
}

extern LeaseStore *gLeaseStore;

static int add_option_iplist(lua_State *L, uint8_t code)
{
    if (lua_gettop(L) != 2 || !lua_islightuserdata(L, 1))
        return 0;
    struct dhcpmsg *dm = static_cast<struct dhcpmsg *>(lua_touserdata(L, 1));
    size_t iplen;
    uint32_t ip;
    if (lua_isstring(L, 2)) {
        const char *ipstr = lua_tolstring(L, 2, &iplen);
        if (inet_pton(AF_INET, ipstr, &ip) != 1)
            return 0;
        add_u32_option(dm, code, ip);
    } else if (lua_istable(L, 2)) {
        union {
            uint32_t ip32;
            uint8_t ip8[4];
        };
        std::string iplist;
        lua_pushnil(L);
        while (lua_next(L, 2) != 0) {
            if (!lua_isstring(L, -1)) {
                lua_pop(L, 1);
                continue;
            }
            const char *ipstr = lua_tolstring(L, -1, &iplen);
            if (inet_pton(AF_INET, ipstr, &ip32) != 1) {
                lua_pop(L, 1);
                continue;
            }
            iplist.push_back(ip8[0]);
            iplist.push_back(ip8[1]);
            iplist.push_back(ip8[2]);
            iplist.push_back(ip8[3]);
            lua_pop(L, 1);
        }
        if (iplist.size())
            add_option_string(dm, code, iplist.c_str(), iplist.size());
    }
    return 0;
}

extern "C" {<commit_msg>Remove undefined behavior from abusing union.<commit_after>#include <stdlib.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <format.hpp>

#include "dhcplua.hpp"
#include "leasestore.hpp"

extern "C" {
#include "options.h"
}

extern LeaseStore *gLeaseStore;

static int add_option_iplist(lua_State *L, uint8_t code)
{
    if (lua_gettop(L) != 2 || !lua_islightuserdata(L, 1))
        return 0;
    struct dhcpmsg *dm = static_cast<struct dhcpmsg *>(lua_touserdata(L, 1));
    size_t iplen;
    uint32_t ip;
    if (lua_isstring(L, 2)) {
        const char *ipstr = lua_tolstring(L, 2, &iplen);
        if (inet_pton(AF_INET, ipstr, &ip) != 1)
            return 0;
        add_u32_option(dm, code, ip);
    } else if (lua_istable(L, 2)) {
        uint32_t ip32;
        char ip8[4];
        std::string iplist;
        lua_pushnil(L);
        while (lua_next(L, 2) != 0) {
            if (!lua_isstring(L, -1)) {
                lua_pop(L, 1);
                continue;
            }
            const char *ipstr = lua_tolstring(L, -1, &iplen);
            if (inet_pton(AF_INET, ipstr, &ip32) != 1) {
                lua_pop(L, 1);
                continue;
            }
            static_assert(sizeof ip8 == sizeof ip32, "size mismatch");
            memcpy(ip8, &ip32, sizeof ip8);
            iplist = std::string(ip8, sizeof ip8);
            lua_pop(L, 1);
        }
        if (iplist.size())
            add_option_string(dm, code, iplist.c_str(), iplist.size());
    }
    return 0;
}

extern "C" {
{'fraction_non_alphanumeric': 0.14186507936507936, 'fraction_numerical': 0.026455026455026454, 'mean_word_length': 2.025, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1112057', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1117, 'n_words': 242}