-
Notifications
You must be signed in to change notification settings - Fork 7.9k
🐛 Fixed Bug #80908 PDO::lastInsertId() return wrong. #6810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -281,6 +281,40 @@ PDO_API zend_string *php_pdo_int64_to_str(int64_t i64) /* {{{ */ | |||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* Convert uint64 to zend_string */ | ||||||
PDO_API zend_string *php_pdo_uint64_to_str(uint64_t i64) /* {{{ */ | ||||||
{ | ||||||
char buffer[65]; | ||||||
char outbuf[65] = ""; | ||||||
register char *p; | ||||||
zend_long long_val; | ||||||
char *dst = outbuf; | ||||||
|
||||||
if (i64 == 0) { | ||||||
return ZSTR_CHAR('0'); | ||||||
} | ||||||
Comment on lines
+293
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could also handle all numbers which are a single digit |
||||||
|
||||||
p = &buffer[sizeof(buffer)-1]; | ||||||
*p = '\0'; | ||||||
|
||||||
while ((uint64_t)i64 > (uint64_t)ZEND_LONG_MAX) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
i64 is already an |
||||||
uint64_t quo = (uint64_t)i64 / (unsigned int)10; | ||||||
unsigned int rem = (unsigned int)(i64 - quo*10U); | ||||||
*--p = digit_vec[rem]; | ||||||
i64 = (int64_t)quo; | ||||||
} | ||||||
long_val = (zend_long)i64; | ||||||
while (long_val != 0) { | ||||||
zend_long quo = long_val / 10; | ||||||
*--p = digit_vec[(unsigned int)(long_val - quo * 10)]; | ||||||
long_val = quo; | ||||||
} | ||||||
while ((*dst++ = *p++) != 0) | ||||||
; | ||||||
*dst = '\0'; | ||||||
return zend_string_init(outbuf, strlen(outbuf), 0); | ||||||
} | ||||||
|
||||||
/* {{{ PHP_MINIT_FUNCTION */ | ||||||
PHP_MINIT_FUNCTION(pdo) | ||||||
{ | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--TEST-- | ||
Bug #80908: pdo_mysql lastInsertId() return wrong, when table id bigger than the maximum value of int64 | ||
--SKIPIF-- | ||
<?php | ||
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) die('skip not loaded'); | ||
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc'); | ||
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); | ||
MySQLPDOTest::skip(); | ||
?> | ||
--FILE-- | ||
<?php | ||
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); | ||
|
||
function createDB(): PDO { | ||
$db = MySQLPDOTest::factory(); | ||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | ||
return $db; | ||
} | ||
|
||
$db = createDB(); | ||
$db->exec('DROP TABLE IF EXISTS test'); | ||
$db->exec('CREATE TABLE test (`id` bigint(20) unsigned AUTO_INCREMENT, `name` varchar(5), primary key (`id`)) ENGINE = InnoDB AUTO_INCREMENT=10376293541461622799'); | ||
|
||
function testLastInsertId(PDO $db) { | ||
echo "Running test lastInsertId\n"; | ||
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); | ||
try { | ||
$db->exec("insert into test (`name`) values ('bar')"); | ||
$id = $db->lastInsertId(); | ||
echo "Last insert id is " . $id . "\n"; | ||
} catch (PDOException $e) { | ||
echo $e->getMessage()."\n"; | ||
} | ||
} | ||
|
||
testLastInsertId($db); | ||
unset($db); | ||
echo "\n"; | ||
|
||
?> | ||
--CLEAN-- | ||
<?php | ||
require __DIR__ . '/mysql_pdo_test.inc'; | ||
MySQLPDOTest::dropTestTable(); | ||
?> | ||
--EXPECT-- | ||
Running test lastInsertId | ||
Last insert id is 10376293541461622799 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding this is pretty meaningless.