Friday, August 30, 2019

reactjs - React routing on click event is not working




I am doing routing on button click event




  redirect(){

return ;

}


Its not working.



my home page- base component:




import React, { Fragment } from 'react';

render() {

const { value } = this.state;

return (


















} />


} />



} />






From my child component I have to redirect to another component,
here is my child component:




  



redirect(){

return ;

}



So but I am not getting any console error too.


Answer



is a component. So, you should use it like this.



class App extends React.Component{
state = {
isRedirect: false,
}


redirect = () => {
this.setState({ isRedirect: true });
}

render() {
return (
<>


{this.state.isRedirect ? : null }


)
}

}



OR




You can use history API



but you should add withRouter()



class App extends React.Component{
render(){
return(
<>



)
}
}

export default withRouter(App);


hard drive - How is a Usb Y cable designed?


Ok,
Recently I have purchased this USB Y cable from Amazon.


Storite USB 2.0 Type A Male to Dual USB A Male Y Splitter Cable Cord Black -50cm


y-cable


I purchased it so I can use it with external hard disk to my 10 year old PC which wasn't able to provide enough USB power to the disk. After getting the cable, I connected it to PC and found it didn't provide enough USB power still.


Then I tried to check the cable with laptop. This laptop is a newer one and one USB port is always enough to run the disk as always (Generally I use USB male to male cable type A to connect this disk to laptop and it works fine).


So, to check the cable..



  1. First, I connected two male plug ends to laptop and third male plug to disk, it works fine.

  2. Second, I connected one usb plug to laptop (the one continuous with thick cable in pic) and other end to disk and it runs fine.

  3. Third, I connected the alternate usb plug to laptop (the one with thin wire in pic) and other end to disk, and now neither the disk rotates nor usb is detected, but disk shows led on which means power connection is active I guess..


From the second test, it is clear that the two male plugs (attached with thick cable in pic) carry both power supply and usb data. Whereas from the third test, it shows the male plug attached to thin cable doesn't carry enough power or usb data, or atleast this part of my cable is damaged I guess..



  • So, I would like to know how are they designed?

  • Are all the three plugs meant to be connected to USB power and data?

  • Is there any way to check if the Y-cable is faulty? I only wanted to check if that cable is faulty so I can replace it if it is..


Answer



How is a USB-Y cable designed?


It is designed for devices which require more power than is available from a single USB port.



Whereas from the third test, it shows the male plug attached to thin cable doesn't carry enough power or usb data



This extra connector does not have any data lines (D-, D+); it only has the power lines (GND, VBUS).




Are all the three plugs meant to be connected to USB power and data?


Yes. The two connectors on the Y end should be connected to the PC/Laptop and the other end to your USB device.




I want to check if the cable is faulty.



First, I connected two male plug ends to laptop and third male plug to disk, it works fine.




  • That is how the cable should be used so it is not faulty


  • The other tests are using the cable in a way it is not meant to be used so the tests are meaningless.





What are the 2 different kinds of Y-Cable?




  • One male connector, two female connectors


    There are two types of the one male two female cables. One is a basic charging cable, splitting one port's
    power across two devices. The other is a specialised cable that
    somehow splits the data lines - and it's only used in very specific
    applications.


  • Two male connectors, one male or one female connector


    What happens is you have your USB cable, one side for the host and
    one for the device, but with an extra connector attached. This extra
    connector does not have any data lines (D-, D+); it only has the power
    lines (GND, VBUS). It's attached in parallel to the existing cable. In
    other words, VBUS is connected to VBUS and GND is connected to GND.




Source Can I safely connect the power-only-end of a USB-Y cable to an other power source? answer by Bob)




Power requirements



Some devices, such as high-speed external disk drives, require more
than 500 mA of current[89] and therefore may have power issues if
powered from just one USB 2.0 port: erratic function, failure to
function, or overloading/damaging the port.


Such devices may come with
an external power source or a Y-shaped cable that has two USB
connectors (one for power and data, the other for power only) to plug
into a computer. With such a cable, a device can draw power from two
USB ports simultaneously.However, USB compliance specification
states that "use of a 'Y' cable (a cable with two A-plugs) is
prohibited on any USB peripheral", meaning that "if a USB peripheral
requires more power than allowed by the USB specification to which it
is designed, then it must be self-powered."



Source USB


php - SQL Injection through mysql_query

I'm working on a site that has been hacked through SQL Injection (at first glance only db entries are corrupted with cross-site scripting) the potential vulnerability I found after looking at the code is that there's a lot of mysql_query call whose inputs are not escaped at all.



The good old :



$query = "SELECT * FROM mytable where name LIKE '%".$_GET['name']."%'"; /*HACK HERE*/
mysql_query($query, $connection);



Nevertheless I can't find how can we do something cool from that injection vulnerability (by cool I mean something like an INSERT or an UPDATE). I've tried to build a statement like this one :



SELECT * FROM mytable where name LIKE '%' AND WHERE id IN (INSERT INTO secondtable (id,description) VALUES (15, 'Fifteenth description');--%'


No success. I guess that the INSERT has nothing to do here.



I'm escaping all user's inputs in the code right now but I've not really get how hackers have penetrated this site, then I'm not 100% sure that my fix will do the job. Any brilliant suggestions ?




Thanks

Java Thread.sleep() on Windows 10 stops in S3 sleep status

There's a desktop application that uses Thread.sleep() to achieve long (minutes or hours) delays. This same application has been working fine from Windows XP through (at least) Windows 7. The application calculates how far in the future it needs to do something, then hits a Thread.sleep(msToWait). This has been working fine, even if the system happens to go into S3 sleep state during the wait.



As of Windows 10, though, the code after Thread.sleep() does not execute "on time" if the machine has been in S3. It appears that the machine begins executing code at "msToWait" plus the time the machine has been in S3 (not 100% sure of this right now, but likely).



Earlier versions of Windows did not exhibit this behavior; code after Thread.sleep() waited the right amount of time, irrespective of sleep status.



Testing has been on the current JVM 1.7.



Is this a Windows 10 bug? Is this a JVM bug? Is there a work-around?




ADDITIONAL DATA:



A test program and procedure were developed. The procedure is to run the program, cause the machine to sleep for about a minute, then wake the machine and wait for the program to finish.



If this the program is run on Windows 10 (reporting as 8) with JVM Version: 25.40-b25, it fails:



C:\Users\Tester\Downloads>SleepTester.exe
Wed Apr 01 10:47:35 PDT 2015 Using default number of minutes: 5
Wed Apr 01 10:47:35 PDT 2015 You can use "SleepTester -minutes 10" to have it sleep for 10 minutes, for example.

Wed Apr 01 10:47:35 PDT 2015 JVM Version: 25.40-b25 Windows Version: Windows 8
Wed Apr 01 10:47:35 PDT 2015 The program will now wait for 5 minutes. Expect wrap-up at Wed Apr 01 10:52:35 PDT 2015
Wed Apr 01 10:53:38 PDT 2015 The system has come through the Thread.sleep(300000).
Wed Apr 01 10:53:38 PDT 2015 This should be a low number: 63589
Wed Apr 01 10:53:38 PDT 2015 This appears to be operating incorrectly...the expected sleep time has NOT been achieved.
Wed Apr 01 10:53:38 PDT 2015 Program is ending.


If the process is run on Windows 7, it does not fail.




Wed Apr 01 17:12:18 EDT 2015 Java Runtime Version: 1.8.0_31-b13 JVM Version: 25.31-b07 Windows Version: Windows 7
Wed Apr 01 17:12:18 EDT 2015 The program will now wait for 6 minutes. Expect wrap-up at Wed Apr 01 17:18:18 EDT 2015
Wed Apr 01 17:18:18 EDT 2015 The system has come through the Thread.sleep(360000).
Wed Apr 01 17:18:18 EDT 2015 This should be a low number: 0
Wed Apr 01 17:18:18 EDT 2015 Program is ending.


This is the test program:



import java.util.Date;


public class SleepTester {

private static int mMinutes;
private static int mDefault = 5;

public static void main(String[] args) throws Exception {
for (int iArg = 0; iArg < args.length; ++iArg) {
if (args[iArg].equals("-minutes") && (iArg + 1) < args.length) {
mMinutes = Integer.parseInt(args[++iArg]);

}
}

if (mMinutes == 0) {
mMinutes = mDefault;
System.out.println(new Date() + " Using default number of minutes: " + mDefault);
System.out.println(new Date() + " You can use \"SleepTester -minutes 10\" to have it sleep for 10 minutes, for example.");
}

System.out.println(new Date() + " Java Runtime Version: " + System.getProperty("java.runtime.version") + " JVM Version: " + System.getProperty("java.vm.version") + " Windows Version: " + System.getProperty("os.name"));

long msDelay = mMinutes * 60 * 1000;
long wakePoint = new Date().getTime() + msDelay;
System.out.println(new Date() + " The program will now wait for " + mMinutes + " minutes. Expect wrap-up at " + new Date(wakePoint));
Thread.sleep(msDelay); // If the machine goes into S3 during this interval, it should not matter, as long as it's awake when it fires.
System.out.println(new Date() + " The system has come through the Thread.sleep(" + msDelay + "). ");
long msAccuracy = Math.abs(new Date().getTime() - wakePoint);
System.out.println(new Date() + " This should be a low number: " + msAccuracy);
if (msAccuracy > 1000) System.out.println(new Date() + " This appears to be operating incorrectly...the expected sleep time has NOT been achieved.");
System.out.println(new Date() + " Program is ending.");
}

}


I realize I could try various other methods to sleep, but I thought that since I went through and documented this, I'd post it here before trying other things.



Additional Information: This failure seems also to appear in Windows 8 (but not 7 or prior).



ADDITION 4/4/2019



The issue is visible on bugs.java.com at the following url JDK-8221971.




There are a few earlier bugs linked to that bug. A comment from the linke JDK-8146730 bug:




17-04-2017 Any news on this topic?



04-04-2019 It has been deferred. It is a low priority, and complex, issue no one is actively assigned to it.


How do I use arrays in C++?



C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector since C++98 and std::array since C++11), so the need for arrays does not arise quite as often as it does in C. However, when you read legacy code or interact with a library written in C, you should have a firm grasp on how arrays work.



This FAQ is split into five parts:





  1. arrays on the type level and accessing elements

  2. array creation and initialization

  3. assignment and parameter passing

  4. multidimensional arrays and arrays of pointers

  5. common pitfalls when using arrays



If you feel something important is missing in this FAQ, write an answer and link it here as an additional part.




In the following text, "array" means "C array", not the class template std::array. Basic knowledge of the C declarator syntax is assumed. Note that the manual usage of new and delete as demonstrated below is extremely dangerous in the face of exceptions, but that is the topic of another FAQ.




(Note: This is meant to be an entry to C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)


Answer





An array type is denoted as T[n] where T is the element type and n is a positive size, the number of elements in the array. The array type is a product type of the element type and the size. If one or both of those ingredients differ, you get a distinct type:




#include 

static_assert(!std::is_same::value, "distinct element type");
static_assert(!std::is_same::value, "distinct size");


Note that the size is part of the type, that is, array types of different size are incompatible types that have absolutely nothing to do with each other. sizeof(T[n]) is equivalent to n * sizeof(T).



Array-to-pointer decay




The only "connection" between T[n] and T[m] is that both types can implicitly be converted to T*, and the result of this conversion is a pointer to the first element of the array. That is, anywhere a T* is required, you can provide a T[n], and the compiler will silently provide that pointer:



                  +---+---+---+---+---+---+---+---+
the_actual_array: | | | | | | | | | int[8]
+---+---+---+---+---+---+---+---+
^
|
|
|

| pointer_to_the_first_element int*


This conversion is known as "array-to-pointer decay", and it is a major source of confusion. The size of the array is lost in this process, since it is no longer part of the type (T*). Pro: Forgetting the size of an array on the type level allows a pointer to point to the first element of an array of any size. Con: Given a pointer to the first (or any other) element of an array, there is no way to detect how large that array is or where exactly the pointer points to relative to the bounds of the array. Pointers are extremely stupid.



Arrays are not pointers



The compiler will silently generate a pointer to the first element of an array whenever it is deemed useful, that is, whenever an operation would fail on an array but succeed on a pointer. This conversion from array to pointer is trivial, since the resulting pointer value is simply the address of the array. Note that the pointer is not stored as part of the array itself (or anywhere else in memory). An array is not a pointer.



static_assert(!std::is_same::value, "an array is not a pointer");



One important context in which an array does not decay into a pointer to its first element is when the & operator is applied to it. In that case, the & operator yields a pointer to the entire array, not just a pointer to its first element. Although in that case the values (the addresses) are the same, a pointer to the first element of an array and a pointer to the entire array are completely distinct types:



static_assert(!std::is_same::value, "distinct element type");


The following ASCII art explains this distinction:



      +-----------------------------------+

| +---+---+---+---+---+---+---+---+ |
+---> | | | | | | | | | | | int[8]
| | +---+---+---+---+---+---+---+---+ |
| +---^-------------------------------+
| |
| |
| |
| | pointer_to_the_first_element int*
|
| pointer_to_the_entire_array int(*)[8]



Note how the pointer to the first element only points to a single integer (depicted as a small box), whereas the pointer to the entire array points to an array of 8 integers (depicted as a large box).



The same situation arises in classes and is maybe more obvious. A pointer to an object and a pointer to its first data member have the same value (the same address), yet they are completely distinct types.



If you are unfamiliar with the C declarator syntax, the parenthesis in the type int(*)[8] are essential:




  • int(*)[8] is a pointer to an array of 8 integers.


  • int*[8] is an array of 8 pointers, each element of type int*.





C++ provides two syntactic variations to access individual elements of an array.
Neither of them is superior to the other, and you should familiarize yourself with both.



Pointer arithmetic




Given a pointer p to the first element of an array, the expression p+i yields a pointer to the i-th element of the array. By dereferencing that pointer afterwards, one can access individual elements:



std::cout << *(x+3) << ", " << *(x+7) << std::endl;


If x denotes an array, then array-to-pointer decay will kick in, because adding an array and an integer is meaningless (there is no plus operation on arrays), but adding a pointer and an integer makes sense:



   +---+---+---+---+---+---+---+---+
x: | | | | | | | | | int[8]
+---+---+---+---+---+---+---+---+

^ ^ ^
| | |
| | |
| | |
x+0 | x+3 | x+7 | int*


(Note that the implicitly generated pointer has no name, so I wrote x+0 in order to identify it.)



If, on the other hand, x denotes a pointer to the first (or any other) element of an array, then array-to-pointer decay is not necessary, because the pointer on which i is going to be added already exists:




   +---+---+---+---+---+---+---+---+
| | | | | | | | | int[8]
+---+---+---+---+---+---+---+---+
^ ^ ^
| | |
| | |
+-|-+ | |
x: | | | x+3 | x+7 | int*
+---+



Note that in the depicted case, x is a pointer variable (discernible by the small box next to x), but it could just as well be the result of a function returning a pointer (or any other expression of type T*).



Indexing operator



Since the syntax *(x+i) is a bit clumsy, C++ provides the alternative syntax x[i]:



std::cout << x[3] << ", " << x[7] << std::endl;



Due to the fact that addition is commutative, the following code does exactly the same:



std::cout << 3[x] << ", " << 7[x] << std::endl;


The definition of the indexing operator leads to the following interesting equivalence:



&x[i]  ==  &*(x+i)  ==  x+i



However, &x[0] is generally not equivalent to x. The former is a pointer, the latter an array. Only when the context triggers array-to-pointer decay can x and &x[0] be used interchangeably. For example:



T* p = &array[0];  // rewritten as &*(array+0), decay happens due to the addition
T* q = array; // decay happens due to the assignment


On the first line, the compiler detects an assignment from a pointer to a pointer, which trivially succeeds. On the second line, it detects an assignment from an array to a pointer. Since this is meaningless (but pointer to pointer assignment makes sense), array-to-pointer decay kicks in as usual.



Ranges




An array of type T[n] has n elements, indexed from 0 to n-1; there is no element n. And yet, to support half-open ranges (where the beginning is inclusive and the end is exclusive), C++ allows the computation of a pointer to the (non-existent) n-th element, but it is illegal to dereference that pointer:



   +---+---+---+---+---+---+---+---+....
x: | | | | | | | | | . int[8]
+---+---+---+---+---+---+---+---+....
^ ^
| |
| |
| |

x+0 | x+8 | int*


For example, if you want to sort an array, both of the following would work equally well:



std::sort(x + 0, x + n);
std::sort(&x[0], &x[0] + n);


Note that it is illegal to provide &x[n] as the second argument since this is equivalent to &*(x+n), and the sub-expression *(x+n) technically invokes undefined behavior in C++ (but not in C99).




Also note that you could simply provide x as the first argument. That is a little too terse for my taste, and it also makes template argument deduction a bit harder for the compiler, because in that case the first argument is an array but the second argument is a pointer. (Again, array-to-pointer decay kicks in.)


PHP Session Security





What are some guidelines for maintaining responsible session security with PHP? There's information all over the web and it's about time it all landed in one place!


Answer



There are a couple of things to do in order to keep your session secure:




  1. Use SSL when authenticating users or performing sensitive operations.

  2. Regenerate the session id whenever the security level changes (such as logging in). You can even regenerate the session id every request if you wish.


  3. Have sessions time out

  4. Don't use register globals

  5. Store authentication details on the server. That is, don't send details such as username in the cookie.

  6. Check the $_SERVER['HTTP_USER_AGENT']. This adds a small barrier to session hijacking. You can also check the IP address. But this causes problems for users that have changing IP address due to load balancing on multiple internet connections etc (which is the case in our environment here).

  7. Lock down access to the sessions on the file system or use custom session handling

  8. For sensitive operations consider requiring logged in users to provide their authenication details again


windows 7 - Does my processor support PAE?


I currently have the following system specifications:


Windows 7 64-Bit


Athlon 64 X2 (W) 4600+ 2.4 GHz


8GB of RAM


500 GB HD


I'm planning on installing Xubuntu as a dual partition along side my current windows installation. On the official Xubuntu page it says:



Your processor needs to support PAE in order to run Xubuntu.



From the microsoft website:



PAE gives 32-bit processors the ability to use more than 4 GB of
physical memory on capable versions of Windows, and is a prerequisite
for NX.



Just for clarification seeing as I have 8GB of RAM and running Windows 7 64-Bit, PAE shouldn't be an issue, correct?


Answer



Every 64-bit x86 CPU supports PAE. There is no 64-bit x86 without NX and there is no NX without PAE.


hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...