News for hackers. Stuff that matters.

jrziviani

The way Edmond escapes the prison was the best thing I've ever read.
Re:Wildfires Are Raging Outside Every Major City in Australia on Thursday January 02, 2020 @11:20AM (#21936649)
I don't know. But Europe did that for centuries in Latin America, Africa, India, and got very rich with that. Ok, they didn't know how dangerous it could be.

But now Europe has the 'moral authority' to prevent poor countries from explore their own lands to give a decent life to their citizens, that have been left in misery after decolonization.

BTW, Europe talk so loud about climate changes, sign agreements and so on but do nothing relevant about it. Do you know why?

Re:Wildfires Are Raging Outside Every Major City in Australia on Thursday January 02, 2020 @10:04AM (#21935867)
Is Macron saying anything about "world intervention" as well?

Otherwise it might look like Europe is only interested in forests that compensate their CO2 emissions.

Should I stop burning CO2? nahhh, it's too expensive. Let's pretend we care about others forests and blame on them.

Re:Wildfires Are Raging Outside Every Major City in Australia on Thursday January 02, 2020 @09:55AM (#21935804)
https://en.wikipedia.org/wiki/Dry_season ...and check out the references.
Re:Wildfires Are Raging Outside Every Major City in Australia on Thursday January 02, 2020 @09:12AM (#21935444)
Not even close to the noise made here: Macron (and Hollywood artists) lying about "world lung", Europeans threatening us with economic sanctions, fake news everywhere (old pictures, fake pictures (from other places), animals that don't even exist in Amazon...
Re:Wildfires Are Raging Outside Every Major City in Australia on Thursday January 02, 2020 @07:38AM (#21934885)
Why aren't Europe, Greta, Hollywood artists screaming their head off just like they did at Brazil/Amazon Forest on June?
Re:Letter to My Fellow Software Engineers on Sunday October 20, 2019 @09:26PM (#21308641)
Great advice! I just want to bold that "Save your money, invest, continually try to increase your salary" part.

Almost every country on Earth is good to wealthy people, so find your way to make money and learn to invest it right. Use the system on your own favor and stop trying to fight against it (because you won't win).

Usually, development countries (I live in Brazil, that's more or less like India) have great opportunities (growth rate) but you need to open yourself to them. More important than have skills is how to make them useful.

Best regards!

Re:A static_cast is not always just a pointer adjustment on Saturday April 02, 2016 @04:06PM (#11413141)
Yes, you'll get a segmentation fault, consider this code, compiled with debug information:

  #include <iostream>

  using namespace std;

  class myclass
  {
      public:
          virtual int sum(int a, int b)
          {
              return a + b;
          }
  };

  int main()
  {
      myclass test;
      cout << test.sum(1, 2) << endl;
      cout << ((myclass*)0)->sum(10, 11) << endl;
      return 0;
  }
Debugging:

  (gdb) b main
  Breakpoint 1 at 0x4008ae: file test.cpp, line 16.
  (gdb) run
  Starting program: /tmp/test 

  Breakpoint 1, main () at test.cpp:16
  16	      myclass test;
  (gdb) ni
  17	      cout << test.sum(1, 2) << endl;
  (gdb) info vtbl test
  vtable for 'myclass' @ 0x400a30 (subobject @   0x7fffffffe410):
  [0]: 0x400974 <myclass::sum(int, int)>
Notice that the function address to be called is not know at compile time, the compiler wrote a lookup table, named vtable, to know which function it will call based on the class instance.

  (gdb) disassemble main
  [snip]
   0x00000000004008ae <+8>: movq  $0x400a30,-0x10(%rbp) ; see the vtable address here
   => 0x00000000004008b6 <+16>:	lea  -0x10(%rbp),%rax ; ohh, the vtable address is the 'myclass test' instance*
   0x00000000004008ba <+20>:	mov    $0x2,%edx
   0x00000000004008bf <+25>:	mov    $0x1,%esi
   0x00000000004008c4 <+30>:	mov    %rax,%rdi ; pass this to RDI
   0x00000000004008c7 <+33>:	callq  0x400974 <myclass::sum(int, int)>
* it means that the first (and only one, in this case) field of my myclass is that vtable.

  (gdb) print test
  $1 = {_vptr.myclass = 0x400a30 <vtable for myclass+16>}
  (gdb) info register rax
  rax   0x7fffffffe410	140737488348176
  (gdb) x/x 0x7fffffffe410
  0x7fffffffe410:  0x00400a30
  (gdb) x/x 0x00400a30
  0x400a30 <_ZTV7myclass+16>:	0x00400974
Then, the compiler knows that it must call '[0]: 0x400974 <myclass::sum(int, int)>' function.

Compare with:

   ((myclass*)0)->sum(10, 11)
   0x00000000004008e5 <+63>:	mov    $0x0,%eax    ; vtable address is 0 :(
   0x00000000004008ea <+68>:	mov    (%rax),%rax  ; the address to RAX is 0, and we cannot access that segment of memory - BOOOM
   0x00000000004008ed <+71>:	mov    (%rax),%rax
   0x00000000004008f0 <+74>:	mov    $0xb,%edx
   0x00000000004008f5 <+79>:	mov    $0xa,%esi
   0x00000000004008fa <+84>:	mov    $0x0,%edi
   0x00000000004008ff <+89>:	callq  *%rax
Re:A static_cast is not always just a pointer adjustment on Saturday April 02, 2016 @03:23PM (#11412920)
I didn't understand why my point was downvoted, anybody who likes to know how things work on backstage or likes to hack stuff would find it interesting.

Sure, undefined behavior can assume anything, but the compiler writer still has to respect the ABI, right?

I was reading the Itanium C++ ABI draft and the only thing I found about it is:

"If a class has a non-virtual destructor, and a deleting destructor is emitted for that class, the deleting destructor must correctly handle the case that the this pointer is NULL. All other destructors, including deleting destructors for classes with a virtual destructor, may assume that the this pointer is not NULL." https://mentorembedded.github.io/cxx-abi/abi.html

However, you can see that I don't have an instance, there is no object so the destructor won't be called. It means that the compiler writer won't probably consider checking 'this' pointer for null - clang/gcc both don't care:

  % g++-4.9 -Wall -O3 -std=c++11 test.cpp -o test; ./test
  0
  21

  % clang++ -Wall -O3 -std=c++11 test.cpp -o test; ./test 
  0
  21

  0x400886 <main()>    push   %rbp
  0x400887 <main()+1>  mov    %rsp,%rbp                          
  0x40088a <main()+4>  mov    $0xb,%edx
  0x40088f <main()+9>  mov    $0xa,%esi
  0x400894 <main()+14> mov    $0x0,%edi
  0x400899 <main()+19> callq  0x400910 <myclass::sum(int, int)>
The 'this' pointer is just a parameter in the register EDI.

NOTE: I don't recommend anyone writing code like this. I'm always concerned about UB and I agree with you. But it's still interesting to know how things really are.

Re:A static_cast is not always just a pointer adjustment on Saturday April 02, 2016 @09:23AM (#11411408)
It's possible to have 'this' == NULL. Consider the code:

  #include <iostream>

  using namespace std;

  class myclass
  {
      public:
          int sum(int a, int b)
          {
              cout << this << endl;
              return a + b;
          }
  };

  int main()
  {
      cout << ((myclass*)0)->sum(10, 11) << endl;
      return 0;
  }
this will print: 0 21

And it's not a null pointer dereference because in C++ it's the function responsible to know the class its belongs to. It means that this code:

  ((myclass*)0)->sum(10, 11)
should become:

  sum(0 /*this*/, 10, 11)
and not

  0->sum(10, 11)
It can be UB in the C++ specification, but considering the systemv abi, 'this' is just a parameter that will be pushed in the call stack before calling 'sum'.