Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
DELETE CASCADE
Oct 25, 2017

i haven't washed my penis since i jerked it to a phtotograph of george w. bush in 2003

leper khan posted:

Lol at saying C doesn't have anonymous functions.

Someone's never seen someone malloc some space, toss some machine code into it, then return the pointer.

Kill me.

hobbesmaster posted:

C lets you do whatever you want, it owns

C absolutely does not let you do this. malloc returns a data pointer. casting a data pointer to a function pointer (so that you can call your block of machine code) is undefined behavior. the C standard is very clear about this, there is no room for interpretation. if you do it anyway and it happens to work, you are just lucky. it could stop working on any platform at any time for any reason or no reason at all

Adbot
ADBOT LOVES YOU

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

DELETE CASCADE posted:

C absolutely does not let you do this. malloc returns a data pointer. casting a data pointer to a function pointer (so that you can call your block of machine code) is undefined behavior. the C standard is very clear about this, there is no room for interpretation. if you do it anyway and it happens to work, you are just lucky. it could stop working on any platform at any time for any reason or no reason at all

Works fine in K&R on all platforms that were contemporary to K&R

akadajet
Sep 14, 2003

DELETE CASCADE posted:

C absolutely does not let you do this. malloc returns a data pointer. casting a data pointer to a function pointer (so that you can call your block of machine code) is undefined behavior. the C standard is very clear about this, there is no room for interpretation. if you do it anyway and it happens to work, you are just lucky. it could stop working on any platform at any time for any reason or no reason at all

get a load of this nerd

CPColin
Sep 9, 2003

Big ol' smile.
Sudden flashback to a Star Wars webcomic where R2D2 says that everything not forbidden is allowed and C3P0 says something like "What is this Microsoft bullshit?"

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
"undefined behavior" includes possibly being able to do it with consistent results, as I have done in the write routine of a flash memory driver that needed to copy itself out of flash into ram and then jump to itself for the actual writing

gonadic io
Feb 16, 2011

>>=
how else am I gonna run an arbitrary payload in your stupid as hoc parser

DELETE CASCADE
Oct 25, 2017

i haven't washed my penis since i jerked it to a phtotograph of george w. bush in 2003

leper khan posted:

Works fine in K&R on all platforms that were contemporary to K&R

there is nothing anywhere in K&R that casts a data pointer to a function pointer or even suggests that this might be possible

Xarn
Jun 26, 2015

gonadic io posted:

Ask me how I know that inline(always) does not mean always

You were smart and read the docs before using it?

gonadic io
Feb 16, 2011

>>=

Xarn posted:

You were smart and read the docs before using it?

hey it's not inline(mostly)

TheFluff
Dec 13, 2006

FRIENDS, LISTEN TO ME
I AM A SEAGULL
OF WEALTH AND TASTE
https://gist.github.com/tp7/e12143e48503f19398f0

code:
# -*- coding: utf-8 -*-
import mmap
import ctypes


def run():
    code_stream = mmap.mmap(-1, 4096, mmap.MAP_PRIVATE,
        mmap.PROT_EXEC | mmap.PROT_READ | mmap.PROT_WRITE)
    code_stream.write(b"\x01\xf7")  # add edi, esi
    code_stream.write(b"\x89\xf8")  # mov eax, edi
    code_stream.write(b"\xc3")  # ret

    code_address = ctypes.addressof(ctypes.c_void_p.from_buffer(code_stream))
    function_type = ctypes.CFUNCTYPE(ctypes.c_uint32, ctypes.c_int32, ctypes.c_int32)
    add_numbers = function_type(code_address)
    assert add_numbers(121, 27) == 148


if __name__ == "__main__":
    run()

DELETE CASCADE
Oct 25, 2017

i haven't washed my penis since i jerked it to a phtotograph of george w. bush in 2003
obviously non-portable. i specified ansi C in my original wordspost for a reason :shobon:

DELETE CASCADE
Oct 25, 2017

i haven't washed my penis since i jerked it to a phtotograph of george w. bush in 2003
yes yes i know posix does it for dlsym(). it's still considered an unportable extension. but the questionable ability to turn an arbitrary block of data into a function doesn't somehow give you the power to program in a closure-based style in C, we still haven't solved any of the memory management issues

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

CPColin posted:

I've been through the lambda on a func with no name

pseudorandom name
May 6, 2007

DELETE CASCADE posted:

C absolutely does not let you do this. malloc returns a data pointer. casting a data pointer to a function pointer (so that you can call your block of machine code) is undefined behavior. the C standard is very clear about this, there is no room for interpretation. if you do it anyway and it happens to work, you are just lucky. it could stop working on any platform at any time for any reason or no reason at all

tell me you're a C standard committee member without telling me you're a C standard committee member

sb hermit
Dec 13, 2016





if it runs (and passes all the tests) then ship it

sb hermit
Dec 13, 2016





You can write anything you want in a commit as long as you understand that someone will eventually hack a dev's computer, pull the repo, and publish it for the world to see. Or someone will hack into a webserver via yet another zero day flaw in wordpress, and pull all the git repos.

Xarn
Jun 26, 2015
Who needs automatic memory management, when you can just yolo things with manual memory management in C++ lambdas.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
c has had closures for years

in macOS/iOS

redleader
Aug 18, 2005

Engage according to operational parameters

DELETE CASCADE posted:

C absolutely does not let you do this. malloc returns a data pointer. casting a data pointer to a function pointer (so that you can call your block of machine code) is undefined behavior. the C standard is very clear about this, there is no room for interpretation. if you do it anyway and it happens to work, you are just lucky. it could stop working on any platform at any time for any reason or no reason at all

pathetic, cowardly behavior by the c standardization committee. it's my memory, i should be able to interpret it however i want

Presto
Nov 22, 2002

Keep calm and Harry on.

DELETE CASCADE posted:

C absolutely does not let you do this. malloc returns a data pointer. casting a data pointer to a function pointer (so that you can call your block of machine code) is undefined behavior. the C standard is very clear about this, there is no room for interpretation. if you do it anyway and it happens to work, you are just lucky. it could stop working on any platform at any time for any reason or no reason at all

So what you're saying is that C totally lets you do it, it just doesn't promise that it will work.

Achmed Jones
Oct 16, 2004



gonadic io posted:

Typically in those cases it's whatever your untrusted executable has smuggled in as a payload and has swapped with the stack pointer wants

The_Franz
Aug 8, 2003

DELETE CASCADE posted:

C absolutely does not let you do this. malloc returns a data pointer. casting a data pointer to a function pointer (so that you can call your block of machine code) is undefined behavior. the C standard is very clear about this, there is no room for interpretation. if you do it anyway and it happens to work, you are just lucky. it could stop working on any platform at any time for any reason or no reason at all

true, you should use mmap with PROT_EXEC (or whatever the windows VirutalAlloc equivalent is). dosbox had a bug due to this at some point

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Presto posted:

So what you're saying is that C totally lets you do it, it just doesn't promise that it will work.

Undefined behavior is just ambiguous and unspecified. Nothing wrong with it or using it, your code may just be slightly less portable.

This is one of the reasons I still have compilers from the 80s and early 90s sitting around.

Portability generally isn't the primary concern. Running on target hardware is.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

leper khan posted:

Undefined behavior is just ambiguous and unspecified. Nothing wrong with it or using it, your code may just be slightly less portable.

No, that's "unspecified behaviour". "Undefined behaviour" is different.

The big problem is that if part of your code exhibits undefined behaviour, then the compiler is under no obligation to ensure that the rest of your code actually does what it says it does.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
isn't it assumed that approximately zero useful c codebases are devoid of undefined behaviour?

Kazinsal
Dec 13, 2011

Doom Mathematic posted:

The function with no name is actually credited as "Joe".

malloc smithee

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

pokeyman posted:

isn't it assumed that approximately zero useful c codebases are devoid of undefined behaviour?

I think most large codebases will have pieces of them that exhibit undefined behaviour on certain inputs, but not always inputs that happen in practice. But that's okay! (In fact, that's kind of the point of having that behaviour be undefined - the compiler is allowed to assume that it never happens instead of having to prove that it never happens).

It's only a problem if the thing that the compiler assumes never happens, actually happens.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
yeah, there's very big gaps between "this function would hit ub given certain inputs but it's never called with those", "this function hits ub in rare scenarios but the happy-path is well defined", and "this function's happy path is ub". the first is perfectly fine. the second is the source of "impossible" bugs that you can never figure out how to repro and close the bug report with "let's pretend this went away" after a few years. basically all c programs have some of these. the third means that you aren't writing c; you're writing in an ad-hoc c-like language that one specific compiler presumably compiles as desired.

Deffon
Mar 28, 2010

This is a good video about why it could be worse if certain cases of undefined behaviour were changed to implementation defined behaviour. https://m.youtube.com/watch?v=yG1OZ69H_-o

CPColin
Sep 9, 2003

Big ol' smile.
I was relieved recently when I realized that I didn't care about a certain, probably impossible combination of inputs to a function, because it meant I could drop in a "this combination is undefined" comment and move on with my day.

redleader
Aug 18, 2005

Engage according to operational parameters
other undefined operations in c include:
An unmatched ' or " character is encountered on a logical source line during tokenization (6.4).

git apologist
Jun 4, 2003

Gentle Autist posted:


it’s confusing for lots of reasons,

echinopsis
Apr 13, 2004

by Fluffdaddy
one thing I heartily enjoy with vscode is how I can alt tab switch around poo poo and come back to it half way waiting for me to finish typing what I was doing


gently caress this bullshit that I was dealing with affinity photo the other night and I was trying to copy a rgb value without hex and so I was trying to alt tab between the apps remembering one number at a time, but fuckin affinity as soon as you lose focus it drops the rgb picker and so you cannot do what I want it to do.

vschode does it and I love it for it

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

echinopsis posted:

one thing I heartily enjoy with vscode is how I can alt tab switch around poo poo and come back to it half way waiting for me to finish typing what I was doing


gently caress this bullshit that I was dealing with affinity photo the other night and I was trying to copy a rgb value without hex and so I was trying to alt tab between the apps remembering one number at a time, but fuckin affinity as soon as you lose focus it drops the rgb picker and so you cannot do what I want it to do.

vschode does it and I love it for it

idk i just tried typing half of a command into the f1 palette and it went away when i alt-tabbed

lord fifth
Dec 26, 2019

LUCK ???
all i know about lambders is my freshman year data structures old dilf professor begrudgingly taught their implementation in java because "i guess that's just what's hip nowadays, i don't get it though"

a function without a name ..... so sad .... ):

DELETE CASCADE
Oct 25, 2017

i haven't washed my penis since i jerked it to a phtotograph of george w. bush in 2003
people used to object-oriented languages don't generally see the need for closures, because you can do all the same stuff with classes and object constructors. it tends to be quite more verbose though, in my opinion. also involves more non-local mutability which is harder to reason about

mystes
May 31, 2006

DELETE CASCADE posted:

people used to object-oriented languages don't generally see the need for closures, because you can do all the same stuff with classes and object constructors. it tends to be quite more verbose though, in my opinion. also involves more non-local mutability which is harder to reason about
You think using closures for private members like in JavaScript is easier to reason about?

lord fifth
Dec 26, 2019

LUCK ???

DELETE CASCADE posted:

people used to object-oriented languages don't generally see the need for closures, because you can do all the same stuff with classes and object constructors. it tends to be quite more verbose though, in my opinion. also involves more non-local mutability which is harder to reason about

yeah that was pretty much his stance. just told us to use them in place of writing all the boilerplate to implement an anonymous functional interface
he's also been teaching the intro java stuff for like, 15 years, so yeah he has objects brain. they teach you language paradigms n stuff completely later

lord fifth
Dec 26, 2019

LUCK ???
just occurred to me that it is possible some yosposters a generation above me had this professor in their undergrad and that's both cool and scary. i remember a few of y'all went to my university

edit: remember as in i saw it lurking a while ago

Adbot
ADBOT LOVES YOU

DELETE CASCADE
Oct 25, 2017

i haven't washed my penis since i jerked it to a phtotograph of george w. bush in 2003

mystes posted:

You think using closures for private members like in JavaScript is easier to reason about?

if it means nobody can mutate those private members out from under me, then yes. unfortunately in javascript it does not mean that :mad:

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply