在AtomicReference getAndSet和其他此类方法中,字段valueOffSet的作用是什么(What is role of field valueOffSet in AtomicReference getAndSet and other such methods)
我正在研究非阻塞算法。 我意识到对volatile和Atomic *等领域的研究对此至关重要。 所以这是一个问题。
在AtomicReference中 , compareAndSet使用unsafe.compareAndSwapObject(this,valueOffset,expect,update) 。 另一个,臭名昭着的lazySet使用unsafe.putOrderedObject(this,valueOffset,newValue); 。
我假设在这些方法中的某个地方,期望和更新对象的比较将使用“equals”来完成。 想知道它是如何以原子方式完成的。我想,最终使用CAS,在某种程度上......我想,valueOffset在那里有一个角色。
不幸的是,Unsafe的源代码不可用..而且这段代码对于理解世界上的valueOffset及其作用至关重要。 任何人有任何想法吗? 谢谢!I am studying non blocking algorithms. I realize that study of areas like volatile and Atomic* are crucial for this. So here is a question.
In AtomicReference, compareAndSet uses unsafe.compareAndSwapObject(this, valueOffset, expect, update). Another one, the infamous lazySet uses unsafe.putOrderedObject(this, valueOffset, newValue);.
I assume somewhere in these methods, a comparison of expect and update object will be done using "equals". Wonder how it is done atomically.. I guess, using CAS eventually, in some way.. and I guess, valueOffset has a role there.
Unfortunately, source code of Unsafe is not available.. and this code appears rather crucial to understand what in the world is valueOffset and what does it do. Anyone any idea please? Thanks!
原文:https://stackoverflow.com/questions/36000545
最满意答案
你的问题很常见。
许多学习者无法从包含四个字母“NULL”的字符串中分辨出
NULL
值。 而后者绝对没有特殊意义。还有一个坏消息:HTTP协议是基于文本的。 意味着使用HTTP方法不能发送除字符串之外的任何内容,因此无法通过HTTP POST发送
NULL
值。要使此代码有效,您必须在服务器端添加NULL,就像您为密码所做的那样:
function post($POST) { $POST = array_map('trim', $_POST); $hash = "$2y$10$"; $salt = "nekiludistringzahash22"; $his = $hash . $salt; $POST['pass'] = crypt($_POST['pass'],$his); $POST['reg_time'] = NULL; $POST['id'] = NULL; return $POST; }
但是,这不是您的代码的主要问题。 最糟糕的消息是你的代码非常容易受到sql注入的攻击 ,尽管看似正确的绑定。
为了使其安全,请使您的函数接受允许字段列表 ,并为缺少的字段分配
NULL
值:function post($allowed) { $post = array(); foreach ($allowed as $key) { if (isset($_POST[$key])) $post[$key] = trim($_POST[$key]); else $_POST[$key] = NULL; } $hash = "$2y$10$"; $salt = "nekiludistringzahash22"; $his = $hash . $salt; $POST['pass'] = crypt($_POST['pass'],$his); }
然后像这样调用它:
$post = post(array('id','uname','pass','name','lname','reg_time')); $sql = "insert into users values(:" . implode(",:", array_keys($post)).");"; $reg = $db->prepare($sql); $reg->execute($post);
这样您就可以过滤掉所有字段名称。
作为奖励,您将能够为提交按钮分配自定义值;)
Your problem is quite common.
Many learners cannot tell a
NULL
value from a string that consists of four letters "NULL". While the latter has absolutely no special meaning.And also there are bad news: HTTP protocol is text-based. Means one cannot send anything but a string using an HTTP method, and thus a
NULL
value cannot be sent over HTTP POST.To make this code work, you have to add your NULLs at the server side, just like you are doing it for the password:
function post($POST) { $POST = array_map('trim', $_POST); $hash = "$2y$10$"; $salt = "nekiludistringzahash22"; $his = $hash . $salt; $POST['pass'] = crypt($_POST['pass'],$his); $POST['reg_time'] = NULL; $POST['id'] = NULL; return $POST; }
However, it is not the main problem with your code. The worst news is that your code is severely vulnerable to sql injection, despite the seemingly proper binding.
To make it safe, make your function accept the list of allowed fields, and assign a
NULL
value to absent ones:function post($allowed) { $post = array(); foreach ($allowed as $key) { if (isset($_POST[$key])) $post[$key] = trim($_POST[$key]); else $_POST[$key] = NULL; } $hash = "$2y$10$"; $salt = "nekiludistringzahash22"; $his = $hash . $salt; $POST['pass'] = crypt($_POST['pass'],$his); }
And then call it like this:
$post = post(array('id','uname','pass','name','lname','reg_time')); $sql = "insert into users values(:" . implode(",:", array_keys($post)).");"; $reg = $db->prepare($sql); $reg->execute($post);
this way you will have all the field names filtered out.
As a bonus, you'll be able to assign a custom value to your submit button ;)
相关问答
更多-
MySQL中有许多日期/时间函数可以在这里实现。 http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add registration_expires=DATE_ADD(NOW(), INTERVAL 7 DAY) 您不能将表达式设置为默认值,但是 - 您需要在INSERT查询中执行此操作。 请注意,即使您的expr值大于1,也没有用于单位值的复数形式。 There are a number o ...
-
我一直在debbuging一段时间,我发现我可以硬编码在我的查询空,然后查询工作正常。 使用硬编码的“null”,查询如下所示: public void insertNewUserToDatabase(String username){ Timestamp timestamp = getCurrentDateTimeInTimeStamp(); String SQL = "INSERT INTO MYDB.USERSUBSCRIPTION (USERNAME, SU ...
-
你得到什么错误信息? 我猜你的实际错误是因为你的php变量没有用引号括起来。 尝试这个 $update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='" .$somename . "'"; What error message are you getting? I'd guess your actual error is because your php variable isn't wrapped in quote ...
-
尝试这个: import MySQLdb import time import datetime ts = time.time() timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') conn = MySQLdb.connect(host= "localhost", user="root", passwd="newpassword", ...
-
你的问题很常见。 许多学习者无法从包含四个字母“NULL”的字符串中分辨出NULL值。 而后者绝对没有特殊意义。 还有一个坏消息:HTTP协议是基于文本的。 意味着使用HTTP方法不能发送除字符串之外的任何内容,因此无法通过HTTP POST发送NULL值。 要使此代码有效,您必须在服务器端添加NULL,就像您为密码所做的那样: function post($POST) { $POST = array_map('trim', $_POST); $hash = "$2y$10$"; $ ...
-
Android:数据库中的时间戳(Android: timestamp in database)[2021-11-18]
您可以将时间戳列添加为: CREATE TABLE [myTable] ( [_id] INTEGER NOT NULL PRIMARY KEY, [timestampColumn] TIMESTAMP ); 你可以像这样回顾它: SELECT datetime(timestampColumn, 'localtime') FROM myTable 编辑: 第一步:为时间戳列声明一个变量 public static final String KEY_TIMESTAMP = "timestampColum ... -
Shakti,你需要解决这个错误。 java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff] 然后看看你格式化日期的方式。 您需要在下面更改您的行 formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); 至: formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ...
-
如果您想在日期时间字段中手动插入日期,则应该将其类型从timestamp (Standard CURRENT TIMESTAMP)更改为datetime 。 只有这样它才能工作,否则总是会节省当前的系统时间。 详细了解日期时间 If you want to manually insert date in timestamp field then you should change its type from timestamp (Standard CURRENT TIMESTAMP) to datetime ...
-
看起来像这样: $query = "INSERT INTO comments ("; $query .= " author, body, page_name, date_Added"; $query .= ") VALUES ("; $query .= " '{$author}', '{$body}', '{$page_name}', NOW()"; $query .= ")"; $result = mysqli_query($connection, $query); 你也可能需要 ALTER TA ...
-
插入时的SimpleDB时间戳(SimpleDB timestamp on insert)[2023-04-22]
我的建议是让一个中间web服务(可能坐在EC2实例上)处理你的iPhone应用程序和SimpleDB之间的所有通信。 这解决了您的问题,因为现在中间Web服务器可以添加一致的时间戳。 但是......更重要的是,这解决了AWS密钥(现在它们生活在您控制的服务器上)的安全问题。 你真的不想把它们嵌入你的iPhone应用...... :-) My recommendation would be to have an intermediate webservice (probably sitting on an ...