<commit_before>#include "poke.h"
#include <nds/dma.h>
#include <nds/arm9/video.h>

#define VRAM_F_SIZE (16*1024)
#define VRAM_G_SIZE (16*1024)
#define VRAM_H_SIZE (32*1024)
#define VRAM_I_SIZE (16*1024)

static bool pointerInRange(volatile void *needle, volatile void *base, size_t size);

Poke::Poke() : size(0), mode(PM_NOOP) {}
Poke::Poke(uint8_t val, volatile uint8_t *addr_) : size(sizeof(uint8_t)), mode(PM_INT), addr(addr_), value8(val) {
}
Poke::Poke(uint16_t val, volatile uint16_t *addr_) : size(sizeof(uint16_t)), mode(PM_INT), addr(addr_), value16(val) {
}
Poke::Poke(uint32_t val, volatile uint32_t *addr_) : size(sizeof(uint32_t)), mode(PM_INT), addr(addr_), value32(val) {
}
Poke::Poke(std::unique_ptr<uint8_t[]> &&dataPtr, size_t dataSize, hwPtr addr_) : size(dataSize), mode(PM_MEMCPY), addr(addr_), valuePtr(std::move(dataPtr)) {
}

Poke::Poke(uint8_t val, uint8_t mask, volatile uint8_t *addr_) : size(sizeof(uint8_t)), mode(PM_BITFIELD), addr(addr_), bitField8(val, mask) {
}
Poke::Poke(uint16_t val, uint16_t mask, volatile uint16_t *addr_) : size(sizeof(uint16_t)), mode(PM_BITFIELD), addr(addr_), bitField16(val, mask) {
}
Poke::Poke(uint32_t val, uint32_t mask, volatile uint32_t *addr_) : size(sizeof(uint32_t)), mode(PM_BITFIELD), addr(addr_), bitField32(val,mask) {
}

Poke::Poke(Poke &&p2) : size(p2.size), mode(p2.mode), addr(p2.addr) {
	
	switch(p2.mode) {
		case PM_NOOP:
		break;
		case PM_INT:
			switch(p2.size) {
				case sizeof(uint8_t):
					value8=p2.value8;
				break;
				case sizeof(uint16_t):
					value16=p2.value16;
				break;
				case sizeof(uint32_t):
					value32=p2.value32;
				break;
			}
		break;
		
		case PM_BITFIELD:
			switch (p2.size) {
				case sizeof(uint8_t) :
					bitField8 = std::move(p2.bitField8);
					break;
				case sizeof(uint16_t) :
					bitField16 = std::move(p2.bitField16);
					break;
				case sizeof(uint32_t) :
					bitField32 = std::move(p2.bitField32);
					break;
			}
		break;
		
		case PM_DMA_16:
		case PM_DMA_32:
		case PM_MEMCPY:
			valuePtr=std::move(p2.valuePtr);
		break;
	}
	p2.mode=PM_NOOP;
	p2.size=0;
}


Poke::~Poke() {
	switch(mode) {
		case PM_DMA_16:
		case PM_DMA_32:
		case PM_MEMCPY:
			valuePtr.~unique_ptr();
		break;
		case PM_BITFIELD://no destructor to call
		default:
		break;
	}
}

void Poke::Perform() {
	uint8_t oldVRamMode;

	if (pointerInRange(addr, VRAM_F, VRAM_F_SIZE)) {
		oldVRamMode = VRAM_F_CR;
		vramSetBankF(VRAM_F_LCD);
	} else if (pointerInRange(addr, VRAM_G, VRAM_G_SIZE)) {
		oldVRamMode = VRAM_G_CR;
		vramSetBankG(VRAM_G_LCD);
	} else if (pointerInRange(addr, VRAM_H, VRAM_H_SIZE)) {
		oldVRamMode = VRAM_H_CR;
		vramSetBankH(VRAM_H_LCD);
	} else if (pointerInRange(addr, VRAM_I, VRAM_I_SIZE)) {
		oldVRamMode = VRAM_I_CR;
		vramSetBankI(VRAM_I_LCD);
	}

	switch(mode) {
		case PM_NOOP:
		break;
		case PM_INT:
			switch(size) {
				case sizeof(uint8_t):
					*((volatile uint8_t*)addr)=value8;
				break;
				case sizeof(uint16_t):
					*((volatile uint16_t*)addr)=value16;
				break;
				case sizeof(uint32_t):
					*((volatile uint32_t*)addr)=value32;
				break;
			}
		break;
		case PM_BITFIELD:
			switch (size) {
			case sizeof(uint8_t) :
				bitField8.Poke((volatile uint8_t*)addr);
				break;
			case sizeof(uint16_t) :
				bitField16.Poke((volatile uint16_t*)addr);
				break;
			case sizeof(uint32_t) :
				bitField32.Poke((volatile uint32_t*)addr);
				break;
			}<commit_msg>Crash if size is corrupt<commit_after>#include "poke.h"
#include <nds/dma.h>
#include <nds/arm9/video.h>
#include <cassert>

#define VRAM_F_SIZE (16*1024)
#define VRAM_G_SIZE (16*1024)
#define VRAM_H_SIZE (32*1024)
#define VRAM_I_SIZE (16*1024)

static bool pointerInRange(volatile void *needle, volatile void *base, size_t size);

Poke::Poke() : size(0), mode(PM_NOOP) {}
Poke::Poke(uint8_t val, volatile uint8_t *addr_) : size(sizeof(uint8_t)), mode(PM_INT), addr(addr_), value8(val) {
}
Poke::Poke(uint16_t val, volatile uint16_t *addr_) : size(sizeof(uint16_t)), mode(PM_INT), addr(addr_), value16(val) {
}
Poke::Poke(uint32_t val, volatile uint32_t *addr_) : size(sizeof(uint32_t)), mode(PM_INT), addr(addr_), value32(val) {
}
Poke::Poke(std::unique_ptr<uint8_t[]> &&dataPtr, size_t dataSize, hwPtr addr_) : size(dataSize), mode(PM_MEMCPY), addr(addr_), valuePtr(std::move(dataPtr)) {
}

Poke::Poke(uint8_t val, uint8_t mask, volatile uint8_t *addr_) : size(sizeof(uint8_t)), mode(PM_BITFIELD), addr(addr_), bitField8(val, mask) {
}
Poke::Poke(uint16_t val, uint16_t mask, volatile uint16_t *addr_) : size(sizeof(uint16_t)), mode(PM_BITFIELD), addr(addr_), bitField16(val, mask) {
}
Poke::Poke(uint32_t val, uint32_t mask, volatile uint32_t *addr_) : size(sizeof(uint32_t)), mode(PM_BITFIELD), addr(addr_), bitField32(val,mask) {
}

Poke::Poke(Poke &&p2) : size(p2.size), mode(p2.mode), addr(p2.addr) {
	
	switch(p2.mode) {
		case PM_NOOP:
		break;
		case PM_INT:
			switch(p2.size) {
				case sizeof(uint8_t):
					value8=p2.value8;
				break;
				case sizeof(uint16_t):
					value16=p2.value16;
				break;
				case sizeof(uint32_t):
					value32=p2.value32;
				break;
				default:
					assert(0);
			}
		break;
		
		case PM_BITFIELD:
			switch (p2.size) {
				case sizeof(uint8_t) :
					bitField8 = std::move(p2.bitField8);
					break;
				case sizeof(uint16_t) :
					bitField16 = std::move(p2.bitField16);
					break;
				case sizeof(uint32_t) :
					bitField32 = std::move(p2.bitField32);
					break;
				default:
					assert(0);
			}
		break;
		
		case PM_DMA_16:
		case PM_DMA_32:
		case PM_MEMCPY:
			valuePtr=std::move(p2.valuePtr);
		break;
	}
	p2.mode=PM_NOOP;
	p2.size=0;
}


Poke::~Poke() {
	switch(mode) {
		case PM_DMA_16:
		case PM_DMA_32:
		case PM_MEMCPY:
			valuePtr.~unique_ptr();
		break;
		case PM_BITFIELD://no destructor to call
		default:
		break;
	}
}

void Poke::Perform() {
	uint8_t oldVRamMode;

	if (pointerInRange(addr, VRAM_F, VRAM_F_SIZE)) {
		oldVRamMode = VRAM_F_CR;
		vramSetBankF(VRAM_F_LCD);
	} else if (pointerInRange(addr, VRAM_G, VRAM_G_SIZE)) {
		oldVRamMode = VRAM_G_CR;
		vramSetBankG(VRAM_G_LCD);
	} else if (pointerInRange(addr, VRAM_H, VRAM_H_SIZE)) {
		oldVRamMode = VRAM_H_CR;
		vramSetBankH(VRAM_H_LCD);
	} else if (pointerInRange(addr, VRAM_I, VRAM_I_SIZE)) {
		oldVRamMode = VRAM_I_CR;
		vramSetBankI(VRAM_I_LCD);
	}

	switch(mode) {
		case PM_NOOP:
		break;
		case PM_INT:
			switch(size) {
				case sizeof(uint8_t):
					*((volatile uint8_t*)addr)=value8;
				break;
				case sizeof(uint16_t):
					*((volatile uint16_t*)addr)=value16;
				break;
				case sizeof(uint32_t):
					*((volatile uint32_t*)addr)=value32;
				break;
				default:
					assert(0);
			}
		break;
		case PM_BITFIELD:
			switch (size) {
			case sizeof(uint8_t) :
				bitField8.Poke((volatile uint8_t*)addr);
				break;
			case sizeof(uint16_t) :
				bitField16.Poke((volatile uint16_t*)addr);
				break;
			case sizeof(uint32_t) :
				bitField32.Poke((volatile uint32_t*)addr);
				break;
			default:
				assert(0);
			}
{'fraction_non_alphanumeric': 0.14722780810112038, 'fraction_numerical': 0.04596380350474002, 'mean_word_length': 4.444096950742768, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '6159393', 'n_tokens_mistral': 3515, 'n_tokens_neox': 2959, 'n_words': 518}