Friday, May 8, 2015

Netstat and ifconfig

I just learned a new way to get network interface information, while I am not a permitted SUDO user.


rajesh@linuxbox:~/code$ sudo ifconfig eth1
eth1      Link encap:Ethernet  HWaddr e0:69:95:88:cb:81
          inet addr:192.168.1.95  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::e269:95ff:fe88:cb81/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:24502 errors:0 dropped:0 overruns:0 frame:0
          TX packets:26449 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:27824021 (26.5 MiB)  TX bytes:20140980 (19.2 MiB)
          Interrupt:20 Memory:fe400000-fe420000

rajesh@linuxbox:~/code$ netstat -ie
Kernel Interface table
eth1      Link encap:Ethernet  HWaddr e0:69:95:88:cb:81
          inet addr:192.168.1.95  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::e269:95ff:fe88:cb81/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:24509 errors:0 dropped:0 overruns:0 frame:0
          TX packets:26455 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:27824607 (26.5 MiB)  TX bytes:20141704 (19.2 MiB)
          Interrupt:20 Memory:fe400000-fe420000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:200 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:12624 (12.3 KiB)  TX bytes:12624 (12.3 KiB)

rajesh@linuxbox:~/code$

Tuesday, May 5, 2015

What are all the common undefined behaviors that a C++ programmer should know about?

Pointer

  • Dereferencing a NULL pointer
  • Dereferencing a pointer returned by a "new" allocation of size zero
  • Using pointers to objects whose lifetime has ended (for instance, stack allocated objects or deleted objects)
  • Dereferencing a pointer that has not yet been definitely initialized
  • Performing pointer arithmetic that yields a result outside the boundaries (either above or below) of an array.
  • Dereferencing the pointer at a location beyond the end of an array.
  • Converting pointers to objects of incompatible types
  • Using memcpy to copy overlapping buffers.

Buffer overflows

  • Reading or writing to an object or array at an offset that is negative, or beyond the size of that object (stack/heap overflow)

Integer Overflows

  • Signed integer overflow
  • Evaluating an expression that is not mathematically defined
  • Left-shifting values by a negative amount (right shifts by negative amounts are implementation defined)
  • Shifting values by an amount greater than or equal to the number of bits in the number (e.g. int64_t i = 1; i <<= 72 is undefined)

Types, Cast and Const

  • Casting a numeric value into a value that can't be represented by the target type (either directly or via static_cast)
  • Using an automatic variable before it has been definitely assigned (e.g., int i; i++; cout << i;)
  • Using the value of any object of type other than volatile or sig_atomic_t at the receipt of a signal
  • Attempting to modify a string literal or any other const object during its lifetime
  • Concatenating a narrow with a wide string literal during preprocessing

Function and Template

  • Not returning a value from a value-returning function (directly or by flowing off from a try-block)
  • Multiple different definitions for the same entity (class, template, enumeration, inline function, static member function, etc.)
  • Infinite recursion in the instantiation of templates
  • Calling a function using different parameters or linkage to the parameters and linkage that the function is defined as using.

OOP

  • Cascading destructions of objects with static storage duration
  • The result of assigning to partially overlapping objects
  • Recursively re-entering a function during the initialization of its static objects
  • Making virtual function calls to pure virtual functions of an object from its constructor or destructor
  • Referring to nonstatic members of objects that have not been constructed or have already been destructed

Source file and Preprocessing

  • A non-empty source file that doesn't end with a newline, or ends with a backslash (prior to C++11)
  • A backslash followed by a character that is not part of the specified escape codes in a character or string constant (this is implementation-defined in C++11).
  • Exceeding implementation limits (number of nested blocks, number of functions in a program, available stack space ...)
  • Preprocessor numeric values that can't be represented by a long int
  • Preprocessing directive on the left side of a function-like macro definition
  • Dynamically generating the defined token in a #if expression

To be classified

  • Calling exit during the destruction of a program with static storage duration

Tuesday, April 28, 2015

Memory Alignment

Hardware is complex; this is a simplified explanation.
A typical modern computer might have a 32-bit data bus. This means that any fetch that the CPU needs to do will fetch all 32 bits of a particular memory address. Since the data bus can't fetch anything smaller than 32 bits, the lowest two address bits aren't even used on the address bus, so it's as if RAM is organised into a sequence of 32-bit words instead of 8-bit bytes.
When the CPU does a fetch for a single byte, the read cycle on the bus will fetch 32 bits and then the CPU will discard 24 of those bits, loading the remaining 8 bits into whatever register. If the CPU wants to fetch a 32 bit value that is not aligned on a 32-bit boundary, it has several general choices:
  • execute two separate read cycles on the bus to load the appropriate parts of the data word and reassemble them
  • read the 32-bit word at the address determined by throwing away the low two bits of the address
  • read some unexpected combination of bytes assembled into a 32-bit word, probably not the one you wanted
  • throw an exception
Various CPUs I have worked with have taken all four of those paths. In general, for maximum compatibility it is safest to align all n-bit reads to an n-bit boundary. However, you can certainly take shortcuts if you are sure that your software will run on some particular CPU family with known unaligned read behaviour. And even if unaligned reads are possible (such as on x86 family CPUs), they will be slower.

Ref: http://stackoverflow.com/questions/1584267/understanding-word-alignment/1584282#1584282

Friday, April 24, 2015


This program prints "Empty String".



int a[]="\0";
if (printf("%s", a))
printf("Non Empty");
else
printf("Empty");


 


This is not an Empty String as size of the sting is 2 (Two).

Monday, March 30, 2015

Unified Extensible Firmware Interface

Both BIOS and UEFI are types of firmware for computers.

BIOS-style firmware is (mostly) only ever found on IBM PC compatible computers.
UEFI is meant to be more generic, and can be found on systems which are not in the ‘IBM PC compatible’ class.

  BIOS is not a generic term for all PC firmware, it is a particular type of PC firmware. Your computer has a firmware. If it’s an IBM PC compatible computer, it’s almost certainly either a BIOS or a UEFI firmware.


An operating system that can be booted from a (U)EFI is called a (U)EFI-aware OS, defined by (U)EFI specification.

 In addition to the standard PC disk partition scheme, which uses a master boot record (MBR), UEFI works with a new partitioning scheme: GUID Partition Table (GPT). GPT is free from many of the limitations of MBR. In particular, the MBR limits on the number and size of disk partitions (up to 4 primary partitions per disk, up to 2 TB (2 × 240 bytes) per disk) are relaxed.[23] GPT allows for a maximum disk and partition size of 8 ZB (8 × 270 bytes).





How to check if my system is on BIOS or UEFI.
The easiest way is to check to see if /sys/firmware/efi exists. It does not appear if you booted using traditional BIOS.
  #!/bin/bash [ -d /sys/firmware/efi ] && echo UEFI || echo BIOS


http://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface
http://www.slashroot.in/linux-booting-process-step-step-tutorial-understanding-linux-boot-sequence
http://wiki.openwrt.org/doc/techref/bootloader
https://www.happyassassin.net/2014/01/25/uefi-boot-how-does-that-actually-work-then/