/dpt/ - Daily Programming Thread

old thread: What are you working on, Sup Forums?

Other urls found in this thread:

shell2017.picoctf.com:37907/
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
benchmarksgame.alioth.debian.org/u64q/compare.php?lang=sbcl&lang2=gcc
en.wikipedia.org/wiki/Patience_sorting
en.wikipedia.org/wiki/Stooge_sort
en.wikipedia.org/wiki/Spaghetti_sort
twitter.com/SFWRedditGifs

it means the variable you used to iterate the vector is not incrementable

Are you calling erase inside your loop?

fuck. I totally assign a vector to a vector inside the loop.
thanks for the clue.

how does it read the flag file then?
a71@shell-web:~/top/mid/problems/f49abea6827cafa20a035340d3812d09$ ../../../../../../problems/f49abea6827cafa20a035340d3812d09/./justno
Oh. Well the auth file doesn't say no anymore so... Here's the flag: 6882905ccde9eeceac01f98cb0260fc2


#include
#include
#include
#include
#include

int main(int argc, char **argv){
FILE* authf = fopen("../../problems/f49abea6827cafa20a035340d3812d09/auth","r"); //access auth file in ../../../problems/f49abea6827cafa20a035340d3812d09
if(authf == NULL){
printf("could not find auth file in ../../problems/f49abea6827cafa20a035340d3812d09/\n");
return 0;
}
char auth[8];
fgets(auth,8,authf);
fclose(authf);
if(strcmp(auth,"no")!=0){
FILE* flagf;
flagf = fopen("/problems/f49abea6827cafa20a035340d3812d09/flag","r");
char flag[64];
fgets(flag,64,flagf);
printf("Oh. Well the auth file doesn't say no anymore so... Here's the flag: %s",flag);
fclose(flagf);
}else{
printf("auth file says no. So no. Just... no.\n");
}
return 0;
}


i don't even have a file named flag in the directory i made. why does it read the flag file relative to where the executable is, but the auth file relative to where i am?

disassembling a video bios right now.
holy shit I forgot how comfy IDA is.

The flag file is an absolute path, notice it starts with /. The auth file us a relative path(relative to the current working directory).

relative directories and file read permission

so if it was
flagf = fopen("problems/f49abea6827cafa20a035340d3812d09/flag","r");


then it would have been accessed relative to the current working directory, and i would have needed to have the file or it'd be a segmentation fault or something?

That is correct.

Well more specifically, fopen would return NULL and it would segfault on the call to fgets.

How do you feel about higher level conditionals?

if (!(fp = (!strcmp(path, "-")) ? stdin : fopen(path, "r")))
return NULL;

How is this used?

Wrote a function to find the probability of k out of n s-sided die adding up to x

Did you know there's a ~40% chance of 3 out of 5 6-sided die adding up to 7? Now you do.

Actually, Fix doesn't even require polymorphic recursion. It's just a recursive type. The only requirement is that the size of the functor applied to any type is constant. Good thing my language has dependent refinement types to express this.
// without the constraint, this wouldn't type check
Fix : (F : Type -> Type | forall X -> size (F X) = n) -> Type
Fix F = F (Fix F)

// B8 is the 8-bit boolean type
// type families are always unions
// thus this is a tagged union
ListF : Type -> Type -> Type
ListF A X = (t: B8, if t then (A, Box X) else Unit)

// type checks because (size (ListF A X)) does not depend on X, thanks to the Box
List : Type -> Type
List A = Fix (ListF A)

BadListF : Type -> Type -> Type
BadListF A X = (t: B8, if t then (A, X) else Unit)

// doesn't type check because (size (BadListF A X)) depends on X
BadList : Type -> Type
BadList A = Fix (BadListF A)

thanks m80, i really wish capture the flags like this would explain the reason why the various hacks work. they never do. another one i'm curious about is this
shell2017.picoctf.com:37907/
at first i suspected you'd have to inject code through the box but i was able to just run
javascript:var server_res = make_ajax_req(true);

by typing it into the url bar. how the hell does that force their program to actually run that though?? if anyone can explain i'd appreciate it

returning a filebuffer

javascript isn't done on the remote server, it's done on the client(your web browser). You make it do whatever you want.

Gives you stdin if file is "-".

isn't stdin a fd and fopen returns a FILE * ?

stdin is a FILE *

Why is Lisp and functional programming in general making a comeback?

stdin is a FILE *.
0 stdin's fd (or stdin's fd is 0).

This is just pseudocode, by the way. I haven't nailed down the concrete syntax yet.

why would anyone use javascript then? isn't that a massive security vulnerability?

I don't know about functional languages, but talking about Lisp: it was always good, but now we have better implementations and better computers (I'm not saying that Lisp implementations are slow).

it's fine if you don't trust the user input
double sanitize every bit of data received from the user
it's easier to just abort if you detect anything weird

Oh I see. It would be great for simplifying code, but it would sacrifice readability

modding your browser's colour scheme isn't a security vulnerability

I've been trying to learn c after using mostly scripting languages for 15 years. I just can't wrap my head around bits and bytes and hex and binary and shifting xanding xoring. I don't know if it's because I have dyscalculia or if I haven't found the right learning materials. Has anyone else overcome this? If so how?

No, javascript is meant to do dynamic actions on web pages. Like when you click something, something happens.

Any security checking on the input should be done server side.

FP is needed to reach the level of abstraction necessary to make modern programming practical

What are you having trouble with?
Most people have trouble with the pointers part.
You should at least have a basic understanding of how computers work, how the lowest addressable unit of memory is a byte and a byte is comprised of 8 bits and you can perform individual operations on those bits through cheating with bitwise arithmetic.

Daily reminder that wearing programming socks makes you a better programmer.

The bitwise arithmetic is my problem I guess. I learned pointers from go so they're not that foreign to me.

Nowadays applications are rising in complexity. Lisp allows you to build abstractions over abstraction, moving away from the dust you can't evade in Fortran based languages (C=>C++=>Java=>Python).
>But muh performance
Sometimes performance is not everything. Complex applications would be an uneditable peace of spaghetti if they were written in low level languages. Anyways, Lisp is fast; it's comparable to C and JVM's performance using the right declarations.

it's just logic applied to individual bits in a byte/

This is actually a pretty good explanation, even if it's for javascript.

whoops
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

so what would be the safe way to do what they were doing on that webpage?

Not doing that in javascript at all. You would have a login POST form, this sends your username/password to the server. The server would do the check and if it's valid give them an authentication token(which would be set in a cookie).

When the server receives a request that contains an authentication token, it verifies that it's correct(and up to date) and knows the user is "logged in".

>it's comparable to C's performance

to add, you'd also have CSRF tokens to prevent XSS and you'd hopefully be running HTTPS so nobody could snoop the traffic.

>I've never heard of SBCL

i guess i'm just in the dark regarding web anatomy. i'm looking on the wiki page for js and they say there's such a thing as server side js too. how would you control whether JS is run on the server or on the client? like, a website me and a team are working on has javascript and php. the php can't be seen with view-source, but the js can. is js code default client side, but can be overriden to be server side? how would you do that?

node.js is serverside js. it's pretty neat check it out some time. I use Python so I haven't done too much with it other than build architecture for node apps.

hmmm
benchmarksgame.alioth.debian.org/u64q/compare.php?lang=sbcl&lang2=gcc

The server side thing is not important. They mean node.js, some retard said to himself

>hey, here's this horrible shitty language that nobody likes writing(javascript)
>why not use it server side too?

I can't even imagine using js outside of client side fluff scripting, how the fuck do people write an entire software application with it?

Maybe you would complain less if he'd said "comparable to Java's performance" but then again, C programmers are fundamentalists who believe that all software is either 100% fast or 100% slow

i think i get it now. the problem with that web page was that they used javascript to verify the password, and then send if it was correct with ajax. they should've just sent the password with ajax and then used php or something to check the password. thanks for sticking with me

So have anyone used Go and built something?

...

People come to realise that state is a bad thing and that you want to have as much stateless computations as possible. Functional programming allows you to strictly separate between stateful and stateless computation in an effective way.

SBCL is not even close to C.

Clojure is arguably the fastest Lisp since it runs on JVM and it's not even near C.

give me 1 good reason not to learn assembly in the next 30 seconds

>People come to realise that state is a bad thing
*shared mutable state

How can I find the sum of two variables of the type params double[] in c#?

Does this come from the increased hate towards OOP?

>he
stop pretending someone else agrees with you
lisp is a technological
dead end and nobody wants to hear about it you shriveled autist

no idea either, I'd never try

trump is banning OOP. there was a war on the programming reddit b/t neets and p*****s for trump taking "computer programming" off of the automatically accepted professions for h1b's. made me chuckle

Is recursion a meme? Why would you ever use recursion instead of loops? I am a programming newbie, and I am learning about recursion, and while it makes your programs looks more "sophisticated", it can be kind of tricky to implement correctly. So, as a complete newbie, when/why is recursion used?

Some algorithms are easier to express with iteration. Some algorithms are easier to express with recursion. Converting between the two is trivial. Iteration can be converted to tail recursion (and tail recursion can be directly converted back to iteration). General recursion can be converted to iteration using a trampoline.

I can testify that recursion is harmful, the call stack is literally a linked list and linked lists are bad. You should only use languages that statically allocate their procedure activation records.

Aight, thanks for the answer anons.

Some things is easier done with recursion.

Didn't NASA go against recursion in their code base as they want it to be "simple" as possible?

some problems are naturally recursive, like exploring a tree.

check(node, desiredval)
if node.value = desiredval return true;
else return check(node.left, desiredval) || check(node.right, desiredval)

very elegant and intuitive

>Is recursion a meme?

imagine, you're at class and you ask the professor:

"professor, is recursion a meme?"

kys

So today I helped a friend out with some interview questions. They were all pretty easy, but one gave me a bit of trouble.

It was about "beautiful subarrays" or something. You're given a number N and list of numbers and you have to (1) generate all possible slices of that list, and (2) compute how many of those slices have exactly N odd numbers (apparently that makes the thing beautiful or some shit).

Seems straightforward:

for (i = 0; i < array.length; ++i)
for (j = i; j < array.length; ++j)
for (k = i; k

>the call stack is literally a linked list
No, it's not.

Yeah, sorry.

I don't think so. I think it has something to do with the requirements that modern application need to fulfil these days. If you consider a web-server with 10 million requests per second, then you quickly realise that the shared state is a bottleneck in your application.

It's a stack allocator containing a linked list of stack frames.

made me laugh imagining an user doing this

I actually didn't know that user. That is pretty interesting. Thanks for the answer.
Oh cool. I guess I better get good with recursion. So that I make my code more A E S T H E T I C.
Jokes aside, thanks user. Definitely see the usefulness of recursion now that you, and other anons gave me some pretty cool examples.

No, it's a chunk of memory with a pointer. The stack frame allocation is just moving that pointer. It's not a linked list.

i don't even know how you'd traverse a tree iteratively. a bunch of algorithms i don't know how you'd do them without recursion. it isn't the 1990's so unless i get a stack overflow i'd go to recursion as my first way to solve a problem like that

>what is a frame pointer

Fine 2 pointers, it doesn't make it a linked list.

>what is a stack pointer

What's an obscure sorting (or really any) algorithm that could use some parallelism? Something that most cs students won't know about

The frame pointer points to the frame pointer of the previous frame, and so on. Linked list.

>It's a stack allocator containing a linked list of stack frames.
The allocator part is the stack pointer (e.g. ESP).
The linked list part is the frame pointer (e.g. EBP).

en.wikipedia.org/wiki/Patience_sorting

Now get to work.

en.wikipedia.org/wiki/Stooge_sort

I think this would work, note just pseudo code:
lowOdds = 0
highOdds = 0
soln = 0
for i in range |A|:
if A[i] == odd:
highOdds += 1
for i in range |A|:
if A[i] == odd:
lowOdds +=1
highOdds -=1
if lowOdds and highOdds == N:
soln += 2
elif lowOdds ^ highOdds == N:
soln += 1
return soln

en.wikipedia.org/wiki/Spaghetti_sort

Clojure is slower than SBCL.

Eliminates OBOEs.

what about TGWM?

EBP points to current stack frame, not the previous. The previous is pushed on the stack.

So you like performance memes.
C fibonacci's ASM (gcc -S -O2 -s):
pushq %rbp
pushq %rbx
subq $8, %rsp
testl %edi, %edi
je .L6
cmpl $1, %edi
movl %edi, %ebx
je .L7
xorl %ebp, %ebp
jmp .L4
.L5:
cmpl $1, %ebx
je .L11
.L4:
leal -1(%rbx), %edi
call fibonacci
addl %eax, %ebp
subl $2, %ebx
jne .L5
.L1:
addq $8, %rsp
movl %ebp, %eax
popq %rbx
popq %rbp
ret
.L11:
addl $1, %ebp
addq $8, %rsp
movl %ebp, %eax
popq %rbx
popq %rbp
ret
.L6:
xorl %ebp, %ebp
jmp .L1
.L7:
movl $1, %ebp
jmp .L1

SBCL's fib ASM:
; ADF: 48894DF0 MOV [RBP-16], RCX ; no-arg-parsing entry point
; AE3: 488BD9 MOV RBX, RCX
; AE6: 4883EB02 SUB RBX, 2
; AEA: 488BD5 MOV RDX, RBP
; AED: 488D4424F0 LEA RAX, [RSP-16]
; AF2: 4883EC20 SUB RSP, 32
; AF6: 488BCB MOV RCX, RBX
; AF9: 488910 MOV [RAX], RDX
; AFC: 488BE8 MOV RBP, RAX
; AFF: E8D8FFFFFF CALL #x100295DADC
; B04: 480F42E3 CMOVB RSP, RBX
; B08: 488B4DF0 MOV RCX, [RBP-16]
; B0C: 488BDA MOV RBX, RDX
; B0F: 48895DF8 MOV [RBP-8], RBX
; B13: 4883E904 SUB RCX, 4
; B17: 488BD5 MOV RDX, RBP
; B1A: 488D4424F0 LEA RAX, [RSP-16]
; B1F: 4883EC20 SUB RSP, 32
; B23: 488910 MOV [RAX], RDX
; B26: 488BE8 MOV RBP, RAX
; B29: E8AEFFFFFF CALL #x100295DADC
; B2E: 480F42E3 CMOVB RSP, RBX
; B32: 488B5DF8 MOV RBX, [RBP-8]
; B36: 488BCA MOV RCX, RDX
; B39: 4801CB ADD RBX, RCX
; B3C: 488BD3 MOV RDX, RBX
; B3F: B902000000 MOV ECX, 2
; B44: FF7508 PUSH QWORD PTR [RBP+8]
; B47: B8581C5320 MOV EAX, #x20531C58 ; #
; B4C: FFE0 JMP RAX

Where is your god now?

Recursion is ALWAYS a sign of a bad programmer.

If you cannot form your logic iteratively, you have made a mistake in choosing your career and/or hobby.

you have to go back

What is this supposed to show?

The current stack frame is on the stack. EBP points to the current stack frame in the sense that it points to its "old EBP". This "old EBP" points to the previous stack frame (its own "old EBP" to be specific). By offsetting a frame pointer you get the frame's locals, return instruction pointer, and arguments.

I'm getting the error no overload for method Sum takes two arguements with this code Console.WriteLine(_attempt.Sum(1, 2));
static double Sum(double x1, double x2)
{
return x1 + x2;
}

Anybody who can point out the error for me?

the C assembly is far more compact and has way fewer instructions than the SBCL fib

I love a bit of false flagging.

Now go ahead and show us just how much boilerplate you had to insert into your code to tell SBCL to essentially strip everything + hard define your types, etc and then imagine doing that for every single function.

>false flagging
Nice try, brainlet.

Recursion is an incorrect way to program.