by whack · 8y ago · 0 comments
int aa = 0;
#define a (++aa)
if(a == 1 && a == 2 && a == 3) {
printf("pff");
} (let ((b 0))
(symbol-macrolet ((a (incf b)))
(and (= a 1) (= a 2) (= a 3))))
For those not familiar with them, symbol macros let a symbol be expanded to an arbitrary expression, kinda like a clean version of the C preprocessor.That said, I feel that in practice I have to worry more about this strange hidden behaviour in C++ and Python than in C and JavaScript. And even Python is not so bad if you stick to the standard libraries.
In Python 3:
>>> import ctypes
>>> ctypes.memmove(id(5)+24, id(23)+24, 8)
>>> 2
2
>>> 3
3
>>> 2 + 3
23
Or, similarly for Python 2 >>> import ctypes
>>> ctypes.cast(id(4),ctypes.POINTER(ctypes.c_int))[4]=5
>>> 2
2
>>> 2 + 2
5 int a = 1;
void *brute(void *vargp){
while(1) {
a = 1; a = 2; a = 3;
}
}
int main(){
pthread_t tid;
pthread_create(&tid, NULL, brute, NULL);
while(1){
if( a == 1 && a == 2 && a == 3 ){
printf("woo\n");
return;
}else{
printf("fail\n");
}
}
} class Equal {
public:
template <typename T>
bool operator==(const T& eq) { return true; };
All kinds of equality are possible. class a {};
multi sub infix:<==> (a $a, $b) {
True
}
if a == 1 && a == 2 && a == 3 {
say "true!"
}
If you wanted to make an actual iterator out of `a`, you could do: multi sub infix:<==> (a $a, $b) {
++(state $) == $b
}
In that case, `a` would equal 1, 2, and 3 in that order. 5 is right out. my \a = any(1, 2, 3);
if ( a == 1 && a == 2 && a == 3 ) {
say "OMG quantum superposition!!!";
}
For those wondering what's going on: https://docs.perl6.org/type/Junction.html :-) class A {
public static bool operator ==(A a, int b) => true;
public static bool operator !=(A a, int b) => false;
}
var a = new A();
if (a == 1 && a == 2 && a == 3) Console.WriteLine("This works"); a == a
is true, while a + 1 == a + 1
evaluates to false.(edit: precision)
Integer b = a+1;
Integer c = a+1;
b==c
Which by default returns true for a=126 but false for a=127. A bad situation for sure, but thankfully pretty much every static analysis tool will spot that.However, it is possible for that statement to evaluate to true with threads if you manipulate the a at the right times.
private int _a;
public int a { get { _a++; return _a;}} a = Object.new; def a.==(b); true; end
now a == b returns true for all b.return true
The equivalent of "return true" is so powerful (or, alternatively, is so parsimonious in its use of power, depending on your perspective), you can even make it work in Haskell, where these mutation tricks don't work, because you can declare a newtype with a custom declaration of == that is just "true".