Skip to content

Instantly share code, notes, and snippets.

View Shnatsel's full-sized avatar

Sergey "Shnatsel" Davidoff Shnatsel

View GitHub Profile
@Shnatsel
Shnatsel / custom_try_fold.rs
Created October 13, 2023 13:21
An equivalent of `bytes()` using internal iteration
#![feature(try_trait_v2)]
use std::{io::{Write, BufRead}, ops::{Try, ControlFlow}};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let test_file_path = std::env::args_os().nth(1).expect("No input file specified!");
let test_file = std::fs::File::open(test_file_path)?;
let stdout = std::io::stdout();
// Lock the stdout once so we don't have to acquire the lock for every line written, which is slow
let stdout = stdout.lock();
#!/bin/sh
set -e
input="$1"
output1="$(mktemp --tmpdir result_XXXXXXXXXXXXX.ppm)"
output2="$(mktemp --tmpdir result_XXXXXXXXXXXXX.ppm)"
trap "rm -f "$output1" "$output2"" EXIT
if ! yes | /home/shnatsel/Code/zune-image/target/release/zune --safe --input "$input" --out "$output1" >/dev/null 2>&1; then
@Shnatsel
Shnatsel / data.txt
Created May 3, 2020 12:39
all occurrences of 'const_err' on crates.io
parsec-service-0.1.2/build.rs: const_err,
sta-0.2.5/src/lib.rs: pub fn const_error(errormessage: String, exitcode: i32) -> Error {
sta-0.2.5/src/lib.rs: pub fn const_error_noexit(errormessage: String) -> Error {
sta-0.2.5/src/lib.rs: let error = error::const_error_noexit("This is a sample error, beep beep.".to_string());
core_extensions-0.1.16/src_core_extensions/marker_traits.rs: #[allow(const_err)]
core_extensions-0.1.16/src_core_extensions/marker_traits.rs: #[allow(const_err)]
oaidl-0.2.1/src/lib.rs:#![deny(const_err)]
x509-signature-0.3.0/src/lib.rs: const_err,
betrusted-pac-0.0.1/src/lib.rs:#![deny(const_err)]
gd32vf103-pac-0.2.0/src/lib.rs:#![deny(const_err)]
@Shnatsel
Shnatsel / compare.sh
Last active December 16, 2018 19:06
compare two folders with images visually and output mean squared error for each pair
#!/bin/bash
# Usage:
# ./compare.sh folder1 folder2 | grep -v '^[0-9]\.[0-9]\+e-' | grep -v '^0 ' | sort --numeric-sort --reverse
# filters out images with significant differences and sorts them by mean squared error
cd "$1"
for file in $(find * -type f); do
# http://www.imagemagick.org/Usage/compare/
NORMALIZED_DIFFERENCE=$(compare -metric MSE "$file" ../"$2"/"$file" null: 2>&1 | cut -d '(' -f 2 | cut -d ')' -f 1)
@Shnatsel
Shnatsel / uninitialized_memory_different_every_time.patch
Created August 22, 2018 02:02
Patch for libdislocate so that it clobbers every buffer allocated with malloc() with a different value. This allows checking for memory disclosure vulnerabilities in arbitrary code simply by running the operation to check twice and comparing the output.
--- libdislocator.so.c.pristine 2018-08-22 04:43:10.970904951 +0300
+++ libdislocator.so.c 2018-08-22 04:46:06.028396381 +0300
@@ -64,7 +64,7 @@
/* Canary & clobber bytes: */
#define ALLOC_CANARY 0xAACCAACC
-#define ALLOC_CLOBBER 0xCC
+int alloc_clobber_counter = 0;
#define PTR_C(_p) (((u32*)(_p))[-1])
@Shnatsel
Shnatsel / membleed.rs
Last active August 25, 2018 03:16
trivial testcase to notice uninitialized memory use in Rust
use std::vec;
// Use the system allocator so we can substitute it with a custom one via LD_PRELOAD
use std::alloc::System;
#[global_allocator]
static GLOBAL: System = System;
/// Returns the sum of all values in a vector of uninitialized memory
fn sum_uninitialized() -> u64 {
let mut my_vec: Vec<u64> = Vec::with_capacity(2048);
@Shnatsel
Shnatsel / generic_bisect.py
Created March 26, 2016 15:35
Generic binary search (bisection) in Python
# Generalized binary search that lets you supply a custom comparator.
# Works both in Python 2 and Python 3.
# Based on Python's built-in bisect.bisect() function.
# Copyright 2016, Sergey "Shnatsel" Davidoff <shnatsel@gmail.com>
class BisectRetVal():
LOWER, HIGHER, STOP = range(3)