Skip to content

Commit 408c09a

Browse files
committed
libutil: Delete footgun overloads of get and getOr
To avoid mistakes like the one in cea85e7. These overloads are just asking for trouble.
1 parent c736db5 commit 408c09a

File tree

1 file changed

+9
-0
lines changed
  • src/libutil/include/nix/util

1 file changed

+9
-0
lines changed

src/libutil/include/nix/util/util.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ typename T::mapped_type * get(T & map, const typename T::key_type & key)
214214
return &i->second;
215215
}
216216

217+
/** Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set. */
218+
template<class T>
219+
typename T::mapped_type * get(T && map, const typename T::key_type & key) = delete;
220+
217221
/**
218222
* Get a value for the specified key from an associate container, or a default value if the key isn't present.
219223
*/
@@ -227,6 +231,11 @@ getOr(T & map, const typename T::key_type & key, const typename T::mapped_type &
227231
return i->second;
228232
}
229233

234+
/** Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set. */
235+
template<class T>
236+
const typename T::mapped_type &
237+
getOr(T && map, const typename T::key_type & key, const typename T::mapped_type & defaultValue) = delete;
238+
230239
/**
231240
* Remove and return the first item from a container.
232241
*/

0 commit comments

Comments
 (0)