Skip to content

Commit fd10cc6

Browse files
committed
use NonZeroUSize for capturing cache size
Signed-off-by: Eric Devolder <[email protected]>
1 parent 0c33eff commit fd10cc6

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

cryptoki/src/session/object_management.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::session::Session;
99
use cryptoki_sys::*;
1010
use std::collections::HashMap;
1111
use std::convert::TryInto;
12+
use std::num::NonZeroUsize;
1213

1314
// Search 10 elements at a time
1415
const MAX_OBJECT_COUNT: usize = 10;
@@ -86,12 +87,8 @@ impl<'a> ObjectHandleIterator<'a> {
8687
fn new(
8788
session: &'a Session,
8889
mut template: Vec<CK_ATTRIBUTE>,
89-
cache_size: usize,
90+
cache_size: NonZeroUsize,
9091
) -> Result<Self> {
91-
if cache_size == 0 {
92-
return Err(Error::InvalidValue);
93-
}
94-
9592
unsafe {
9693
Rv::from(get_pkcs11!(session.client(), C_FindObjectsInit)(
9794
session.handle(),
@@ -101,11 +98,11 @@ impl<'a> ObjectHandleIterator<'a> {
10198
.into_result(Function::FindObjectsInit)?;
10299
}
103100

104-
let cache: Vec<CK_OBJECT_HANDLE> = vec![0; cache_size];
101+
let cache: Vec<CK_OBJECT_HANDLE> = vec![0; cache_size.get()];
105102
Ok(ObjectHandleIterator {
106103
session,
107-
object_count: cache_size,
108-
index: cache_size,
104+
object_count: cache_size.get(),
105+
index: cache_size.get(),
109106
cache,
110107
})
111108
}
@@ -187,6 +184,7 @@ impl Session {
187184
/// Iterate over session objects matching a template.
188185
///
189186
/// # Arguments
187+
///
190188
/// * `template` - The template to match objects against
191189
///
192190
/// # Returns
@@ -195,31 +193,34 @@ impl Session {
195193
/// matching the template. Note that the cache size is managed internally and set to a default value (10)
196194
///
197195
/// # See also
196+
///
198197
/// * [`ObjectHandleIterator`] for more information on how to use the iterator
199198
/// * [`Session::iter_objects_with_cache_size`] for a way to specify the cache size
200199
#[inline(always)]
201200
pub fn iter_objects(&self, template: &[Attribute]) -> Result<ObjectHandleIterator> {
202-
self.iter_objects_with_cache_size(template, MAX_OBJECT_COUNT)
201+
self.iter_objects_with_cache_size(template, NonZeroUsize::new(MAX_OBJECT_COUNT).unwrap())
203202
}
204203

205204
/// Iterate over session objects matching a template, with cache size
206205
///
207206
/// # Arguments
207+
///
208208
/// * `template` - The template to match objects against
209-
/// * `cache_size` - The number of objects to cache. Note that 0 is an invalid value and will return an error.
209+
/// * `cache_size` - The number of objects to cache (type is [`NonZeroUsize`])
210210
///
211211
/// # Returns
212212
///
213213
/// This function will return a [`Result<ObjectHandleIterator>`] that can be used to iterate over the objects
214214
/// matching the template. The cache size corresponds to the size of the array provided to `C_FindObjects()`.
215215
///
216216
/// # See also
217+
///
217218
/// * [`ObjectHandleIterator`] for more information on how to use the iterator
218219
/// * [`Session::iter_objects`] for a simpler way to iterate over objects
219220
pub fn iter_objects_with_cache_size(
220221
&self,
221222
template: &[Attribute],
222-
cache_size: usize,
223+
cache_size: NonZeroUsize,
223224
) -> Result<ObjectHandleIterator> {
224225
let template: Vec<CK_ATTRIBUTE> = template.iter().map(Into::into).collect();
225226
ObjectHandleIterator::new(self, template, cache_size)
@@ -229,12 +230,12 @@ impl Session {
229230
///
230231
/// # Arguments
231232
///
232-
/// * `template` - A [Attribute] of search parameters that will be used
233-
/// to find objects.
233+
/// * `template` - A reference to [Attribute] of search parameters that will be used
234+
/// to find objects.
234235
///
235236
/// # Returns
236237
///
237-
/// Upon success, a vector of [ObjectHandle] wrapped in a Result.
238+
/// Upon success, a vector of [`ObjectHandle`] wrapped in a Result.
238239
/// Upon failure, the first error encountered.
239240
///
240241
/// # Note

cryptoki/tests/basic.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use cryptoki::session::{SessionState, UserType};
1616
use cryptoki::types::AuthPin;
1717
use serial_test::serial;
1818
use std::collections::HashMap;
19+
use std::num::NonZeroUsize;
1920
use std::thread;
2021

2122
use cryptoki::mechanism::ekdf::AesCbcDeriveParams;
@@ -399,27 +400,26 @@ fn session_objecthandle_iterator() -> testresult::TestResult {
399400

400401
// test iter_objects_with_cache_size()
401402
// count keys with cache size of 20
402-
let found_keys = session.iter_objects_with_cache_size(&key_search_template, 20)?;
403+
let found_keys = session
404+
.iter_objects_with_cache_size(&key_search_template, NonZeroUsize::new(20).unwrap())?;
403405
let found_keys = found_keys.map_while(|key| key.ok()).count();
404406
assert_eq!(found_keys, 11);
405407

406-
// count keys with cache size of 0 => should result in an error
407-
let found_keys = session.iter_objects_with_cache_size(&key_search_template, 0);
408-
assert!(found_keys.is_err());
409-
410408
// count keys with cache size of 1
411-
let found_keys = session.iter_objects_with_cache_size(&key_search_template, 1)?;
409+
let found_keys = session
410+
.iter_objects_with_cache_size(&key_search_template, NonZeroUsize::new(1).unwrap())?;
412411
let found_keys = found_keys.map_while(|key| key.ok()).count();
413412
assert_eq!(found_keys, 11);
414413

415414
// count keys with cache size of 10
416-
let found_keys = session.iter_objects_with_cache_size(&key_search_template, 10)?;
415+
let found_keys = session
416+
.iter_objects_with_cache_size(&key_search_template, NonZeroUsize::new(10).unwrap())?;
417417
let found_keys = found_keys.map_while(|key| key.ok()).count();
418418
assert_eq!(found_keys, 11);
419419

420420
// fetch keys into a vector
421421
let found_keys: Vec<ObjectHandle> = session
422-
.iter_objects_with_cache_size(&key_search_template, 10)?
422+
.iter_objects_with_cache_size(&key_search_template, NonZeroUsize::new(10).unwrap())?
423423
.map_while(|key| key.ok())
424424
.collect();
425425
assert_eq!(found_keys.len(), 11);
@@ -428,13 +428,15 @@ fn session_objecthandle_iterator() -> testresult::TestResult {
428428
let key1 = found_keys[1];
429429

430430
session.destroy_object(key0).unwrap();
431-
let found_keys = session.iter_objects_with_cache_size(&key_search_template, 10)?;
431+
let found_keys = session
432+
.iter_objects_with_cache_size(&key_search_template, NonZeroUsize::new(10).unwrap())?;
432433
let found_keys = found_keys.map_while(|key| key.ok()).count();
433434
assert_eq!(found_keys, 10);
434435

435436
// destroy another key
436437
session.destroy_object(key1).unwrap();
437-
let found_keys = session.iter_objects_with_cache_size(&key_search_template, 10)?;
438+
let found_keys = session
439+
.iter_objects_with_cache_size(&key_search_template, NonZeroUsize::new(10).unwrap())?;
438440
let found_keys = found_keys.map_while(|key| key.ok()).count();
439441
assert_eq!(found_keys, 9);
440442

0 commit comments

Comments
 (0)