Copilot commented on code in PR #3728:
URL: https://github.com/apache/brpc/pull/3728#discussion_r4538930728


##########
src/brpc/ubshm/ub_ring.cpp:
##########
@@ -1199,7 +1199,7 @@ RETURN_CODE UBRing::UbrMapRemoteShmAddTimer(SHM 
*local_trx_shm, const char *loca
         SHM_NAME_PREFIX,
         local_name,
         SERVER_SHM_NAME_SUFFIX);
-    if (UNLIKELY(result < 0)) {
+    if (UNLIKELY(result < 0 || result >= SHM_MAX_NAME_BUFF_LEN)) {
         LOG(ERROR) << "Copy server shared memory name failed, local_name=%s, 
ret=%d.", local_name, result;

Review Comment:
   This `LOG(ERROR) << ... , local_name, result;` uses the comma operator, so 
`local_name` and `result` are not actually logged (and the `%s/%d` formatting 
is not applied with stream logging). Please switch to stream insertion (e.g., 
`<< local_name << ... << result`) or use a formatting API consistent with your 
logging framework so the log includes the intended values.



##########
src/brpc/ubshm/ub_ring.cpp:
##########
@@ -1260,8 +1260,9 @@ RETURN_CODE UBRing::ApplyAndMapLocalShm(SHM 
*local_trx_shm, const char *local_na
         SHM_NAME_PREFIX,
         local_name,
         CLIENT_SHM_NAME_SUFFIX);
-    if (UNLIKELY(result < 0)) {
+    if (UNLIKELY(result < 0 || result >= SHM_MAX_NAME_BUFF_LEN)) {
         LOG(ERROR) << "Copy client localTrx shared memory name failed, 
local_name=" << local_name << ", ret=" << result;
+        UBRingManager::ReleaseUbrTrxFromMgr(_trx);
         return UBRING_ERR;

Review Comment:
   After releasing `_trx` back to the manager, `_trx` remains as a non-null 
member pointer even though ownership has been relinquished. To avoid a 
dangling/invalid pointer remaining on the object (and reduce risk of accidental 
reuse or double-release in other paths), consider setting `_trx = nullptr` 
immediately after `ReleaseUbrTrxFromMgr(_trx)`.



##########
src/brpc/ubshm/ub_endpoint.h:
##########
@@ -60,6 +60,11 @@ UbrDataFormat PreferredDataFormatForShmType(SHM_TYPE 
shm_type);
 UbrDataFormat SelectDataFormat(UbrDataFormat local_format,
                                uint16_t remote_format_id);
 
+// Builds the server-side shared-memory name derived from a peer name.
+// Returns false when the result cannot fit in shm_name.
+bool BuildServerShmName(const char* peer_name,
+                        char shm_name[SHM_MAX_NAME_BUFF_LEN]);

Review Comment:
   In a function parameter, `char shm_name[SHM_MAX_NAME_BUFF_LEN]` decays to 
`char*` and does not enforce the buffer size at compile time. If this API is 
intended for broader use, consider accepting `(char* shm_name, size_t 
shm_name_len)` (or a safer wrapper type) to make the contract explicit and 
prevent accidental misuse with a smaller buffer.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to